javascript - Repeated countdown -
i'm trying write script in plain javascript counts down 2 specific points in time, 1 after other, repeating every day. i'm not sure how done.
specifically: have lunchtime, , have dinnertime on first day (both being date objects). every day, want following behaviour (more or less).
at lunch-time of first day, "lunch time" text in green. , there's countdown dinner-time of day. , once reach "dinner-time", following
and repeat every day, indefinitely. i'm not sure how done using pure javascript. currently, have handy piece of javascript thus:
times = [lunchtime, dinnertime]; var = new date(); var next = times.pop(); var timeremaining = - next.gettime(); settimeout(stylefxn, timeremaining);
the function stylefxn
following:
function stylefxn(){ //change styling on site }
and i'm able make time switch lunch dinner on first day, never again afterwards. of course, cannot assume lunch-time , dinner-time every day @ same time or have same intervals between each other!
it occurred me use times
array within loop. however, found annoyingly confusing use settimeout within while-loop. specifically:
while((var = times.pop())!=null){ var = new date(); var timerem = now.gettime() - a.gettime(); settimeout(function(){ //do styling }, timerem); }
that didn't work. can me out?
i not sure whether answers question or not think, problem can solved recursing through stylefxn. see following code along comments:
times = [lunchtime, dinnertime]; var = new date(); //you have used pop method have not specified how next lunch or dinner // time added times array // assume have method var next = times.pop(); var timeremaining = - next.gettime(); settimeout(stylefxn, timeremaining); function stylefxn(){ //change styling on site //this assumes have appropriate // styling logic recursion // method add new lunch , // dinner time times array //call function/method update times array = new date(); next = times.pop(); timeremaining = - next.gettime(); //********************************** settimeout(styfxn,timeremaining); //********************************* // process repeat indefinitely }
Comments
Post a Comment