更新:
我正在尝试让 jquery smart poll 为我工作。如果进程在前 2 秒内准备就绪,那么它工作正常,但如果没有,它不会按预期重试。我还想让重试在一定次数后超时。
https://github.com/hmert/jquery-smart-poll
<script type="text/javascript" charset="utf-8">
$("#load_availables").toggle()
$.poll(2000,function(retry){
$.getScript('update_availables.js?job_id=<%= @bed.job_id %>&space_id=<%= @space.id %>', function(response, status){
if (status == 'success')
$("#load_availables").toggle() //works fine if ready
else
retry() //does not retry at all
})
})
</script>
最佳答案
var retryCount = 0;
$.poll(10000, function(retry){
retryCount++;
$.get('something', function(response, status){
if (status == 'success')
// Do something
else
{
if(retryCount < 11) retry();
else return;
}
})
})
我还没有机会测试这个 - 但是,理论上这应该可以完成工作。
顺便说一句,如果您想加快 JavaScript 的效率,请使用 cometd 服务器并使用慢速轮询。看看http://www.ape-project.org/
关于jquery轮询解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5661314/