微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 递归setTimeout模式

在阅读Long Polling上的一篇文章时,我对以下两种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);
})();

根据博客,它说 – 关于第二个片段,

So,this pattern doesn’t guarantee execution on a fixed interval per
se. But,it does guarantee that the prevIoUs interval has completed
before the next interval is called
.

为什么第二个片段保证前一个间隔已经完成?

我知道第一个(事件循环)但很少混淆第二个片段.

最佳答案

Why second snippet guarantee that the prevIoUs interval has completed?

在第一个示例中,无论先前的$.ajax()调用是否完成,都会以一定间隔调用$.ajax().

在第二个示例中,直到$.ajax()的完整函数再次调用poll.

原文地址:https://www.jb51.cc/jquery/427766.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐