Skip to content

Instantly share code, notes, and snippets.

@ccnokes
Last active August 29, 2015 14:03
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 ccnokes/a2877f111d777931d5ad to your computer and use it in GitHub Desktop.
Save ccnokes/a2877f111d777931d5ad to your computer and use it in GitHub Desktop.
Poller object
function Poller(interval, cb, limit) {
var _this = this;
this.interval = interval || 250;
this.poll = null;
this.limit = limit;
var count = 0;
this.poll = setInterval(function() {
if(typeof cb == 'function') {
if(_this.limit) {
count++;
if(count > _this.limit) {
_this.stop();
return false;
}
}
cb(_this.poll);
}
}, this.interval);
}
Poller.prototype.stop = function() {
clearInterval(this.poll);
};
//test it out
var t = new Poller(1000, function(poller) {
console.log('long 1000');
//clearInterval(poller);
}, 2);
console.log(t);
var a = new Poller(250, function(poller) {
console.log(a, 'short 250');
t.stop();
clearInterval(poller);
});
console.log(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment