Recursive template setTimeout

When reading an article about Long Polling, I got a little confused between the following two options for setInterval

1 -

 setInterval(function(){ $.ajax({ url: "server", success: function(data){ //Update your dashboard gauge salesGauge.setValue(data.value); }, dataType: "json"}); }, 30000); 

2-

 (function poll() { setTimeout(function() { $.ajax({ url: "server", success: function(data) { sales.setValue(data.value); }, dataType: "json", complete: poll }); }, 30000); })(); 

As the blog says, he talks about the second fragment,

Therefore, this pattern does not guarantee execution at a fixed interval per se. But ensures that the previous interval is completed before the next interval is called .

Why does the second passage ensure that the previous interval is completed?

I know about the first (event loops), but got a little confused in the second fragment.

+5
source share
1 answer

Why does the second passage ensure that the previous interval is completed?

In the first example, $.ajax() is called at intervals whether the previous call to $.ajax() ends or not.

In the second example, poll not called again until the complete $.ajax() function.

+3
source

All Articles