如何立即开始投票? [英] How to start polling immediately?

查看:66
本文介绍了如何立即开始投票?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用轮询来进行以 15 秒为间隔执行的小检查.

I am trying to use polling for a small check that is performed at an interval of 15 secs.

setInterval(function(){
            $.ajax({ url: "aaa.com",
                success: function(data){
                showUpdate(data);
              }, dataType: "text"});
            }, 15000);

但这意味着在轮询开始之前有 15 秒的初始延迟,这在我的情况下是不希望的.如何强制投票立即开始?

But this means that there is an initial delay of 15 secs, before the polling starts, which is not desired in my case. How can i force the polling to start immediately?

推荐答案

不要在简单的解决方案完成工作时寻找聪明的解决方案:

Don't look for smart solutions when simple ones do the job :

function check() {
   $.ajax({ url: "aaa.com",
     success: function(data){
     showUpdate(data);
   }, dataType: "text"});
}
check();
setInterval(check, 15000);

或者,我通常更喜欢

function check() {
   $.ajax({ url: "aaa.com",
     success: function(data){
        showUpdate(data);
        setTimeout(check, 15000);
   }, dataType: "text"});  
}
check();

因为在延迟响应的情况下不会有一堆调用.

Because there wouldn't be a stack of calls in case of delayed response.

这篇关于如何立即开始投票?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆