Created
August 25, 2012 06:57
-
-
Save hitsujiwool/3461759 to your computer and use it in GitHub Desktop.
clock with changable tick interval
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
/*! | |
* 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