Skip to content

Instantly share code, notes, and snippets.

@yoko
Created January 22, 2009 11:15
Show Gist options
  • Save yoko/50500 to your computer and use it in GitHub Desktop.
Save yoko/50500 to your computer and use it in GitHub Desktop.
Timer = function() {
};
Timer.prototype = {
initialize: function() {
this.time = [];
this.paused = false;
},
stack: function() {
var now = (new Date()).getTime();
this.time.push(now);
return now;
},
start: function() {
this.initialize();
this.stack();
return 0;
},
stop: function() {
var t = this.time;
var length = t.length;
if (length % 2) {
this.stack();
++length;
}
var ret = 0;
for (var i = 0; i < length; i += 2) {
var time = t.slice(i, i + 2);
console.log(t, time);
ret += time[1] - time[0];
}
return ret;
},
record: function() {
return this.time;
},
count: function() {
var t = this.time;
return (new Date()).getTime() - t[t.length - 1];
},
pause: function() {
if (this.paused) return this.paused;
var now = this.stack();
var t = this.time;
var last = t[t.length - 2];
return this.paused = now - last;
},
restart: function() {
if (!this.paused) return;
this.stack();
var last = this.paused;
this.paused = false;
return last;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment