Created
March 5, 2009 11:05
-
-
Save slashdotdash/74299 to your computer and use it in GitHub Desktop.
jQuer Polling Plugin
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
// 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