Skip to content

Instantly share code, notes, and snippets.

@ccnokes ccnokes/poller.js
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.