Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple AJAX Polling jQuery Plugin
//Simple AJAX Polling jQuery Plugin
// example usage:
/* $.ajaxPoll({
url: "/path",
type: "GET",
interval: 250,
maxAttempts: 25,
successCondition: function(result) {
return result != "processing";
},
success: function(data) {
$('#container').replaceWith(data);
}
});
*/
jQuery.ajaxPoll = function(user_options){
var options = {
interval: 30000,
maxAttempts: 15
}
jQuery.extend(options, user_options);
var attempts = 0;
options.success = function(data, status) {
if (options.successCondition(data)) {
if (options.successCallback){
options.successCallback(data, status);
}
return;
}
attempts++;
if (attempts > options.maxAttempts) {
return;
}
setTimeout(function() { jQuery.ajax(options) }, options.interval);
};
jQuery.ajax(options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.