kuhaku kuhaku - 1 year ago 67
Javascript Question

About javascript runtime behavior and interval

When there's an interval method running the background, can it cause the current function to stop in the middle, go do the interval method and then return to the current function?

For example:

setInterval(bar, 1000);

function foo(){


Is it possible that when reaching
for example to go and do
will execute only after the current function (
) is done?

PS: I'm asking about how it will work on Chrome.

Answer Source

No, it's not possible. The interval is added to the events loop (queue) and new item from this event queue is only picked up when the call stack is empty. This means that the foo should finish executing before new item is processed from the queue. Moreover, it's not only functions added through interval won't get executed until foo is finished, it's all other things like UI events and network request callbacks.

Here is a good talk that explains what this queue is.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download