Reputation: 13
I need to run a code between 9 AM and 10 AM. The code should not run before 9 AM nor after 10 AM. But the code should stay active between these times. The code will be like changing the background colour of a div element.
How do I implement this in Javascript?
Upvotes: 1
Views: 104
Reputation: 1586
I see you have react tag, So I assume that you want to render a div in react. You can use inline background color to set it.
This code return a div that have red color between 9AM and 10AM. Otherwise, It has blue color.
render() {
const hour = new Date().getHours();
const background = hour >= 9 && hour <= 10 ? 'red' : 'blue'
return (
<div style={{backgroundColor: background}}></div>
)
}
Upvotes: 1