Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created December 5, 2012 13:47
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tanepiper/4215634 to your computer and use it in GitHub Desktop.
Save tanepiper/4215634 to your computer and use it in GitHub Desktop.
A high resolution timer, set the tick duration (default 1s) and callback to be actioned on each tick - accurate to within ~1-5ms per tick and compensates automatically for drift over time.
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);
}
});
@lowjumpingfrog
Copy link

Thanks for writing this timer it is exactly what I need. Before I "borrow" I need to completely understand this:

var HighResolutionTimer = window.HighResolutionTimer = window.HighResolutionTimer || (function() { ... }());

It seems like this var declaration is trying to avoid "this" from referring to the window object in the description of the HighResolutionTimer object?
I am fairly new to javascript and I learn a lot from reading bits of code like this because I see new uses of the language that I could never of come up with on my own initially.
Thanks

@SilentImp
Copy link

for some reason .stop just don't work

@jmesserer
Copy link

I'm seeing the same behavior ... stop isn't working.

Source

  _timer = HighResolutionTimer(
    duration: 1000
    callback: (timer) ->
      seconds = ((1000 / timer.duration) * timer.total_ticks) % 60
      console.log seconds
      if seconds == 5
        timer.stop()
        console.log 'stopping timer ...'
  )  

  console.log 'starting timer ...'
  _timer.run()

Output

starting timer ... 
0 
1 
2 
3 
4 
5 
stopping timer ... 
6 
7 
...

@jvlppm
Copy link

jvlppm commented Oct 18, 2016

I ported this timer to TypeScript, if anyone is interested in, but I did not include support for using native HighResolutionTimer provided by some browsers.

https://gist.github.com/jvlppm/b4fd92e4579d59d0a9ea5656b865e0d2

@evgenyfedorenko
Copy link

Stop did not work for me either. I needed to create private property stopped:boolean and set it to true in stop method, then check it before create new timeout next time in run method. The problem with clearing the timeout is that you are not really clearing it for entire process, you are clearing it for the single setTimeout but the next time it creates the new one and it starts over.

@srghma
Copy link

srghma commented Feb 1, 2019

example is wrong

it should be

 var _timer = new 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