Last active
August 29, 2015 14:03
-
-
Save ccnokes/a2877f111d777931d5ad to your computer and use it in GitHub Desktop.
Poller object
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
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