有一个页面,我想定期做背景Ajax请求。因此,页面加载,那么它应该发在一定的时间Ajax请求。
There is a page and I want periodically to make "background" ajax requests. So the page is loaded then it should send ajax requests in a certain amount of time.
我会用cron了点。我从来没有使用previously所以我不知道是否会适合这项任务。有没有其他更简单的方法?
I might use cron for that. I have never use previously so I'm wondering if it would fit for that task. Is there any other more simple way?
P.S。的时间延迟将是约5分钟。
P.S. The time delay will be about 5 minutes.
由于在本质上是时间之间未知的延迟,你送出去,你收到一个完整的响应AJAX请求的时间,一个常常更好的方法是开始下一个AJAX调用时间固定金额的在的事先之一结束。这样一来,你也可以确保您的通话不重叠。
Since there is essentially an unknown delay between the time you send out an AJAX request and the time you receive a complete response for it, an oftentimes more elegant approach is to start the next AJAX call a fixed amount of time after the prior one finishes. This way, you can also ensure that your calls don't overlap.
var set_delay = 5000,
callout = function () {
$.ajax({
/* blah */
})
.done(function (response) {
// update the page
})
.always(function () {
setTimeout(callout, set_delay);
});
};
// initial call
callout();
下一篇:替代轮询的REST服务?REST