1

我按顺序创建了一系列 $.post,其中下一个 $.post 在加载完成时作为回调加载。

 $.post("test", function() {
      alert("success");
    })
    .complete(function() { 
         //LOAD NEXT SEQUENCE OF POST HERE
    });

我的问题是,如果我的第一个序列在 2 分钟内加载,那么我需要等待 2 分钟才能看到下一篇文章。我想要做的是即使第一个序列尚未完成,五秒钟后,将执行下一个加载。我已经阅读了有关 $.ajax 超时的信息,但我不希望它停止执行来自服务器的调用......我只想确保在 5 秒超时后加载下一个序列。

这可能吗?

谢谢你。

4

2 回答 2

3

您可以使用 setInterval 函数

function Yourfunction()
{
    //here you need to do post one after other 
    $.post("test", function() {
      alert("success");
    }).complete(function() { 
     //Here if your interval is not elapsed and completed triggered then your next post will be loaded.
     //LOAD NEXT SEQUENCE OF POST HERE/ 
    });
}
setInterval(Yourfunction, 5000);
于 2012-10-06T00:57:14.207 回答
1

您只需将该方法包装在 setTimeout 中,并在您想要开始下一次尝试时调用它。

var timer;
function attemptNext()
{
    timer = setTimeout(tryPost, 5000);
}

function tryPost()
{
    clearTimeout(timer);
    attemptNext('MY URL');
    $.post("test", function() {
        alert("success");
    })
    .complete(function() { 
        tryPost();
    });
}
于 2012-10-06T00:59:31.593 回答