jQuery, simple polling example

Mike picture Mike · Jul 26, 2011 · Viewed 125.4k times · Source

I'm learning jQuery, and I'm trying to find a simple code example that will poll an API for a condition. (ie, request a webpage every few seconds and process the results)

I'm familiar with how to do AJAX in jQuery, I just can't seem to find the "proper" way of getting it to execute on a "timer".

Answer

Johnny Craig picture Johnny Craig · Jul 26, 2011
function doPoll(){
    $.post('ajax/test.html', function(data) {
        alert(data);  // process results here
        setTimeout(doPoll,5000);
    });
}