javascript - Why the fiddle is not working and there is a time lag behind in following case? -


my fiddle link follows:http://jsfiddle.net/fvyys/2/

the function call follows:

 load_timer('0', '6', '9', 0, '0', '', '0'); 

actually issue fiddle not working. expected behaviour of code timer should decrease second second , reaches zero(i.e. example timer should start @ 00:06:09 , end @ 00:00:00). it's not working here in fiddle. code working in application don't know why code not working in fiddle. 1 more issue noticed in application timer lagging sometime behind. can please me in regard? if need further information i'll provide same. in advance.

your code has following structure :

 var counter = delay;  function loop() {      counter--;      displaytime(delay, counter);      if (counter > 0) {          settimeout( loop, 1000 );      }  } 

2 things :

  • displaytime() execution takes time : example, if takes 0.2 seconds complete, loop executed every 1.2 seconds (instead of every second)
  • settimeout( ..., 1000 ) means "please dear javascript runtime, can run code in 1 second ? if have other stuff do, ok me wait more."
    have guarantee there at least 1 second between settimeout call , loop excution, delay can longer.

if want avoid time drift, check real time on each iteration :

var start = date.now(); function loop() {     var = date.now();     var elapsedtime = - start; //elapsed time in milliseconds     displaytime(delay, elapsedtime);     if (elapsedtime < delay) {         settimeout(loop, 1000);     } } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -