datetime - Javascript Time - Run a Function every Half Hour (Solved) -
i made function 2 things: returns amount of time until next thirty minute mark, , calls halfhour function when timer reaches zero. if time 12:06:27
, output 24:33
(mm:ss)
function checktime() { var time = new date(); var mins = time.getminutes(); mins = (60-mins) % 30; var secs = time.getseconds(); if (secs != 60){ secs = (60-secs) % 60; } else { secs = 00; } time = [number(mins),number(secs)]; if (mins == 0 && secs == 0){ halfhour(); } return time; }
this works, there strange glitch. when minute rolls over, shows... 24:02
24:01
23:00
23:59
23:58
it calls halfhour();
1 minute soon, @ false 0:00
mark in last sequence: 1:02
1:01
0:00
0:59
how can correct this?
edit: commenters dbrin , njzk2 have solution. it's subtracting minutes , seconds 59
instead of 60
. below modified working code. sorry it's messy.
function checktime() { var time = new date(); var mins = time.getminutes(); mins = (59-mins) % 30; var secs = time.getseconds(); if (secs != 60){ secs = (59-secs) % 60; } else { secs = 00; } time = [number(mins),number(secs)]; if (mins == 0 && secs == 0){ halfhour(); } return time; }
thank sharing!
why reinvent wheel when javascript has built in setinterval(code, delay); acheive this.
setinterval(function() { alert("i fire after every 2 seconds") }, 2000);
Comments
Post a Comment