Created
July 30, 2009 01:31
-
-
Save defunkt/158493 to your computer and use it in GitHub Desktop.
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
/* | |
* a smart poller for jquery. | |
* (by github) | |
* | |
* simple example: | |
* | |
* $.smartPoller(function(retry) { | |
* $.getJSON(url, function(data) { | |
* if (data) { | |
* doSomething(data) | |
* } else { | |
* retry() | |
* } | |
* }) | |
* }) | |
* | |
* The $.smartPoller function accepts a starting | |
* interval in ms but defaults to 1000: | |
* | |
* $.smartPoller(2000, function(retry) { | |
* | |
*/ | |
;(function($) { | |
$.smartPoller = function(wait, poller) { | |
if ($.isFunction(wait)) { | |
poller = wait | |
wait = 1000 | |
} | |
(function startPoller() { | |
setTimeout(function() { | |
poller.call(this, startPoller) | |
}, wait) | |
wait = wait * 1.5 | |
})() | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment