Please see the following JavaScript code:
var cis_current_time = 0;
setInterval(function() {
cis_current_time += 1;
},1);
$("#timingInfo").html(cis_current_time);
setTimeout(function() {
$("#timingInfo").html($("#timingInfo").html() + ', ' + cis_current_time);
},1000);
As a result I except to get 0, 1000, but it returns 0, number near 200
Please check a fiddle.
What is the reason of such behavior?


setTimeoutandsetIntervalhave a minimal precision of 4~5 ms I believe (because it actually says "after the current functions, in approximately n milliseconds"), so you won't get it to run each millisecond. It has already been shown on SO, but where...setIntervalcallbacks. If you need to do things based on time passing, you need to useDateobjects orwindow.performance.now(), recording the interval since your callback was last called.