Skip to content

Instantly share code, notes, and snippets.

@handerson
Forked from tanepiper/HighResolutionTimer.js
Created August 26, 2014 14:57
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 handerson/bc25164a375097542cda to your computer and use it in GitHub Desktop.
Save handerson/bc25164a375097542cda to your computer and use it in GitHub Desktop.
var HighResolutionTimer = window.HighResolutionTimer = window.HighResolutionTimer || (function() {
var HighResolutionTimer = function(options) {
this.timer = false;
this.total_ticks = 0;
this.start_time = undefined;
this.current_time = undefined;
this.duration = (options.duration) ? options.duration : 1000;
this.callback = (options.callback) ? options.callback : function() {};
this.run = function() {
this.current_time = Date.now();
if (!this.start_time) { this.start_time = this.current_time; }
this.callback(this);
var nextTick = this.duration - (this.current_time - (this.start_time + (this.total_ticks * this.duration) ) );
this.total_ticks++;
(function(i) {
i.timer = setTimeout(function() {
i.run();
}, nextTick);
}(this));
return this;
};
this.stop = function(){
clearTimeout(this.timer);
return this;
};
return this;
};
return HighResolutionTimer;
}());
var _timer = HighResolutionTimer({
duration: 1000,
callback: function(timer) {
var hours = Math.floor( ( ( (1000 / timer.duration) * timer.total_ticks) / 60) / 24) % 24;
var minutes = Math.floor( ( (1000 / timer.duration) * timer.total_ticks) / 60) % 60;
var seconds = ( (1000 / timer.duration) * timer.total_ticks) % 60;
console.log(hours, minutes, seconds);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment