开发者

Timed AJAX request

I am using jQuery and I have an aAax request as follows;

    $.ajax({
        type: 'POST',
        url: 'test.php',
        data: {
            data: "test_data"
    开发者_Python百科    },
        cache: false,
        success: function(result) {
            if (result == "true"){
                alert("true");
            }else{
                alert("false");
            }
        },
    });

This works well. But, I want to do this in a timely manner, in specific intervals of time. Say, I want to execute this every 30 seconds. The page should not be reloaded. I want this to happen in background, hidden. Anybody know how to do this?


setInterval(function() {
    $.ajax({
        type: 'POST',
        url: 'test.php',
        data: {
            data: "test_data"
        },
        cache: false,
        success: function(result) {
            if (result == "true"){
                alert("true");
            }else{
                alert("false");
            }
        }
    });
}, 30000);


Using setInterval? http://www.w3schools.com/jsref/met_win_setinterval.asp

Or if you only wanted it to run after a success message you could use setTimeout in the success callback to recall the function that sends that ajax request

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜