Skip to content

Instantly share code, notes, and snippets.

@smpb
Created October 31, 2012 23:53
Show Gist options
  • Save smpb/3990731 to your computer and use it in GitHub Desktop.
Save smpb/3990731 to your computer and use it in GitHub Desktop.
jQuery polling strategies
(function poll(){
$.ajax({
url: '/poll',
success: function(data) {
console.log(data);
},
error: function(msg) { console.log('bummer...') },
dataType: 'json',
complete: poll,
timeout: 30000
});
})();
(function poll(interval){
// the first invocation will have an interval of zero
// so we don't wait for the first request, only the next ones
interval = typeof interval !== 'undefined' ? interval : 0;
setTimeout(function(){
$.ajax({
url: '/poll',
success: function(data) {
console.log(data);
// setup the next poll recursively
poll(30000);
},
error: function(msg) { console.log('bummer...'); poll(30000); },
dataType: 'json'
});
}, interval);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment