定期发送Ajax请求 [英] Periodically send ajax requests

查看:154
本文介绍了定期发送Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个页面,我想定期做背景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();

这篇关于定期发送Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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