Skip to content

Instantly share code, notes, and snippets.

@slashdotdash
Created March 5, 2009 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashdotdash/74299 to your computer and use it in GitHub Desktop.
Save slashdotdash/74299 to your computer and use it in GitHub Desktop.
jQuer Polling Plugin
// jQuery Polling Plugin
// http://www.buntin.org/2008/sep/23/jquery-polling-plugin/
(function($) {
$.fn.poll = function(options){
var $this = $(this);
// extend our default options with those provided
var opts = $.extend({}, $.fn.poll.defaults, options);
setInterval(update, opts.interval);
// method used to update element html
function update(){
$.ajax({
type: opts.type,
url: opts.url,
success: opts.success
});
};
};
// default options
$.fn.poll.defaults = {
type: "POST",
url: ".",
success: '',
interval: 2000
};
})(jQuery);
// Usage
$("#chat").poll({
url: "/chat/ajax/1/messages/",
interval: 3000,
type: "GET",
success: function(data){
$("#chat").append(data);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment