recursion - Why does my javascript countdown timer occasionally over-run? -


as part of web-based educational game, have countdown timer using code follows. mostly, timer stops @ zero, occasionally, over-runs, , continues count down 59:59.

a couple of notes code:

  1. counttime global variable set dropdown menu
  2. stopcount gloabal variable set reset button
  3. leadzero external function used formatting

i understand settimeout not accurate, have thought remtime > 0 condition stop recursion eventually, if missed first time.

anyway, here's code:

function newcount() {     var starttime=new date();     var min=starttime.getminutes();     var sec=starttime.getseconds();     var endtime=(min*60)+sec+counttime;     stopcount=false;      countdown();      function countdown() {         var today=new date();         var m=today.getminutes();         var s=today.getseconds();         var currtime=(m*60)+s;         var remtime=endtime-currtime;         var remmin = math.floor(remtime/60);         var remsec = remtime % 60;          // add 0 in front of numbers<10         remmin=leadzero(remmin);         remsec=leadzero(remsec);          document.getelementbyid('timer').innerhtml=remmin+":"+remsec;          if (remtime > 0 && stopcount==false) {              t=settimeout(function(){countdown()},500);         }         else if (stopcount==false){document.getelementbyid("nextbutton").innerhtml = "finished";}         else {}      }    } 

as requested, here code buttons , calling functions ...

buttons:

<button onclick="newsyllable()" id="nextbutton" style="font:60px 'nunito', sans-serif;">start</button>  <button onclick="resetscore()"><span style="font:20px 'nunito', sans-serif;">reset</span></button> 

functions:

function resetscore() {     points=0     stopcount=true;     document.getelementbyid("score").innerhtml = "score: " + points     document.getelementbyid("nextbutton").innerhtml = "start"     document.getelementbyid("syllable").innerhtml = "&nbsp;"     t=settimeout(function(){setcountdown()},500); }      function newsyllable() {     if (document.getelementbyid("nextbutton").innerhtml == "finished"){     }     else {         if (document.getelementbyid("nextbutton").innerhtml == "start"){             newcount();         }          document.getelementbyid("nextbutton").innerhtml = "next"           switch (currentunit) {         case "1":             unit1();             break;         case "2":             unit2();             break;         case "3":             unit3();             break;         case "4":             unit4();             break;         case "5":             unit5();             break;         case "6":             unit6();             break;         }         document.getelementbyid("score").innerhtml = "score: " + points++     } } 

ok think problem originates way you're calculating remaining time using 2 date objects. simpler way use counttime variable starting time (in seconds), , use 1000 millisecond interval perform countdown. try code instead:

var stopcount = false; var counttime = 10;  function newcount() {     if(stopcount === false) {         var counter=setinterval(countdown, 1000);     }     stopcount = true;      function countdown() {         counttime = counttime - 1;         var remmin = math.floor(counttime/60);         var remsec = counttime % 60;         // add 0 in front of numbers<10         remmin=leadzero(remmin);         remsec=leadzero(remsec);         document.getelementbyid('timer').innerhtml=remmin+":"+remsec;          if (counttime <= 0)         {             clearinterval(counter);             document.getelementbyid("nextbutton").innerhtml = "finished";             return;         }     }    } 

jsfiddle: http://jsfiddle.net/5vxbe/8/


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -