Skip to content

Instantly share code, notes, and snippets.

@hitsujiwool
Created August 25, 2012 06:57
Show Gist options
  • Save hitsujiwool/3461759 to your computer and use it in GitHub Desktop.
Save hitsujiwool/3461759 to your computer and use it in GitHub Desktop.
clock with changable tick interval
/*!
* clock
* Copyright (c) 2012 hitsujiwool <utatanenohibi@gmail.com>
* MIT Licensed
*/
;(function(exports) {
function Clock(speed) {
var now = new Date().getTime();
this.current = now;
this.timeWhenChecked = now;
this.speed = speed || 1;
};
Clock.prototype.getSpeed = function(speed) {
return this.speed;
};
Clock.prototype.setSpeed = function(speed) {
this.speed = speed;
};
Clock.prototype.update = function() {
var now = new Date().getTime();
this.current += (now - this.timeWhenChecked) * this.speed;
this.timeWhenChecked = now;
return this;
};
Clock.prototype.getDate = function() {
this.update();
return new Date(this.current);
};
Clock.prototype.getTime = function() {
this.update();
return this.current;
};
exports.Clock = Clock;
})(typeof window !== 'undefined' ? window : module.exports);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment