When using the setInterval
function, it is important to note that a delay of 0 milliseconds is not permitted as there is a minimum delay requirement in place (as stated in the specifications, which mandates a minimum delay of 4ms). For more information, refer to the documentation.
// To execute a function every second:
var timer = setInterval(myFunction, 1000);
// Although specifying 0 doesn't throw an error, a default minimum of 4ms will be applied:
var timer = setInterval(myFunction, 0);
If you want the function to be called initially and then at regular intervals, you should invoke the function when setting the interval:
var timer = setInterval(myFunction, 1000);
myFunction();
The Mozilla documentation highlights the significance of the minimum delay:
"The HTML5 spec specifies a minimum of 4ms timeout value, which has been consistent in browsers since 2010. Prior to this, the minimum timeout value for nested timeouts was 10 ms."
In relation to the slow performance on IE8, the "lag" experienced with setInterval could be due to IE8's inability to keep pace with the function execution. The function is invoked at each interval but IE8's queue may become overwhelmed, leading to delays. Increasing the delay might help alleviate this issue.
As noted by Vasile on this Google Code forum post:
"If a new call is initiated while a prior one is still active, the new call gets queued and executed subsequently; this can lead to slowdowns...(the timer will slow down as the queue grows and performance diminishes over time)"
This issue is frequently observed with low delays in IE8; further insight can be found in this specific post.
Another aspect to consider regarding setInterval delays is that inactive tabs may have different behavior:
"In Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2 and Chrome 11, timeouts are restricted to firing no more frequently than once per second (1000ms) in inactive tabs..."
Refer to this related Stack Overflow post for additional details.