Skip to content

Instantly share code, notes, and snippets.

@dniswhite
Created February 13, 2014 16:53
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 dniswhite/8978995 to your computer and use it in GitHub Desktop.
Save dniswhite/8978995 to your computer and use it in GitHub Desktop.
quick timer implementation, also good demo of clojure
function Timer(callback, delay) {
if (!isNaN(delay) && callback) {
if (delay < 1000) delay = 1000;
this.delay = delay;
this.callback = callback;
var running = false;
this.ticker = function (a) {
a.callback.call(a);
};
this.pause = function () {
if (running) {
window.clearInterval(this.timerId);
running = false;
}
};
this.resume = function () {
if (!running) {
this.timerId = window.setInterval(this.ticker, this.delay, this);
running = true;
}
};
this.resume();
}
return this;
};
count = 0;
var timer = new Timer(function () {
console.log('count - ' + count);
if (count > 3) {
this.pause();
} else {
count++;
}
}, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment