5
$(document).ready(function() {
    (function poll() {
        setTimeout(function() {
            $.ajax({
                url: "/project1/api/getAllUsers",
                type: "GET",
                success: function(data) {
                    console.log("polling");
                },
                dataType: "json",
                complete: poll,
                timeout: 5000
            }), 5000
        });
    })();
});​

This just keeps executing as fast as the server can respond but I was hoping it would only poll every 5 seconds. Any suggestions?

EDIT: I should add, 5 seconds after the request has completed would be preferable.

David Nehme
  • 20,665
  • 7
  • 73
  • 114
chrisjleu
  • 4,085
  • 5
  • 39
  • 55
  • It seems to be executing the next AJAX poll 5 seconds after the previous one has completed. What about that do you want to change? Best I can understand your question, that was what you wanted. – Richard Neil Ilagan Jun 27 '12 at 10:13
  • @Richard Neil Ilagan: I want it to execute roughly every 5 seconds (5 seconds after the ajax request has completed is also fine and I'm told, better practice) but what I observe is that the request executes much more rapidly, as if it were ignoring the 5 second delay. – chrisjleu Jun 27 '12 at 10:33
  • Sorry, just went out for dinner. Took a second look, and found where you seem to be getting it wrong. Popped up an answer below. – Richard Neil Ilagan Jun 27 '12 at 11:41

3 Answers3

7

It seems that you've managed to get your setTimeout delay argument written in the wrong place.

$(document).ready(function() {
  (function poll() {
    setTimeout(function() {
        $.ajax({
            url: "/project1/api/getAllUsers",
            type: "GET",
            success: function(data) {
                console.log("polling");
            },
            dataType: "json",
            complete: poll,
            timeout: 5000
        }) //, 5000  <-- oops.
    }, 5000); // <-- should be here instead
  })();
});​

If you follow the braces, you'll see that you're calling setTimeout like:

setTimeout(function () {
    $.ajax(), 5000
})

and should be

setTimeout(function () {
    $.ajax();
}, 5000)

This should call the AJAX poll 5 seconds after the previous one has completed.

Richard Neil Ilagan
  • 14,133
  • 5
  • 44
  • 64
1

If it should poll every 5 seconds and not necessarily 5 seconds after completing the last request, you could use setInterval. Don't know if that's acceptable, but it would make recursion unnecessary.

function poll() {

            $.ajax({
                url: "/project1/api/getAllUsers",
                type: "GET",
                success: function(data) {
                    console.log("polling");
                },
                dataType: "json"
        });
    }

setInterval(poll, 5000);
Me.Name
  • 11,334
  • 3
  • 25
  • 41
0

Incase you wanted to use jQuery's promise syntax, rather than callback syntax here's another tidy way.

function poll() {
    $.get('http://your-api-endpoint.com')
    .done(function() {
        // 200 - OK response
    })
    .fail(function() {
        // Error Response
    })
    .always(function () {
        setTimeout(function() {
            poll();
        }, 5000);
    });
}

poll();
Luke
  • 3,004
  • 4
  • 33
  • 54