Alfred
Alfred

Reputation: 21406

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"
        },
        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?

Upvotes: 0

Views: 1728

Answers (2)

cvsguimaraes
cvsguimaraes

Reputation: 13260

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);

Upvotes: 2

Alex
Alex

Reputation: 7374

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

Upvotes: 1

Related Questions