Created
October 31, 2012 23:53
-
-
Save smpb/3990731 to your computer and use it in GitHub Desktop.
jQuery polling strategies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function poll(){ | |
$.ajax({ | |
url: '/poll', | |
success: function(data) { | |
console.log(data); | |
}, | |
error: function(msg) { console.log('bummer...') }, | |
dataType: 'json', | |
complete: poll, | |
timeout: 30000 | |
}); | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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