Aurore
Aurore

Reputation: 746

Create a day night cycle

I'm making a website with a timer that adds +1 every second. this value is stored in daynightcycle I would like to trigger day or night every 10 seconds. If the status is "day" it should switch to "night" and if the status is "night" it should switch to "day". So I started with something like :

if(daynightcycle >5 || daynightcycle >10) { //etc...
    console.log {"night"}
}

But this doesn't allow me to check what the previous state was and I feel it's not the correct way to do it. Sorry if it's a very basic question, thanks a lot for the help.

Upvotes: 0

Views: 275

Answers (1)

J. Doe
J. Doe

Reputation: 867

var state = "day"; 
if(daynightcycle % 10 == 0) { //etc...
   console.log(state);
   state = state == "day" ? "night" : "day"; 
}

This will check if daynightcircle is a multiple of 10 and if so prints out the state variable. After the print it will flip the state (day and night).

I hope this is what you want to archive.

Upvotes: 1

Related Questions