mrchimp / tock

Timer Object/Class. Kickass!
MIT License
237 stars 33 forks source link

Add possibility for delaying ticks of timer #18

Closed ghost closed 8 years ago

ghost commented 9 years ago

Hi Need something like: timer.delay(x);

so after it timer will delay next tick for x miliseconds, but just once

I use tock for physic simulation in game, this library is game changer for my project, but to sync physic through network well enough, must be sure that ticks on client and server are almost in same time.

You could do this: timer.pause(); setTimeout(function() { timer.pause(); }, x);

but I know, that it isn't the best way;

Any help?

ghost commented 9 years ago

This may work, but I'm not sure:

function delay(dms) { pause_time = lap() - dms; stop();

if (countdown) { _startCountdown(pause_time); } else { _startTimer(pause_time); } }

mrchimp commented 9 years ago

That sounds interesting. I'm glad to hear someone is finding Tock useful! Is it your Liero clone that you are using it in? I used to love that game!

I am away on holiday at the moment so I can't get too involved but I will look into it properly when I get back home (early next year).

I think the way to do it would be to add an offset() method that would take a positive or negative number and attach that value to the Tock object. Then in _tick() add this number to next_interval_in. If you want to test this out and send me a pull request I'll merge it when I get a chance.

ghost commented 9 years ago

I used to love that game too, but more his clone Gusanos with good mods. Anyway delaying will not be needed. If you do something every 20ms, you can't sync ticks properly because of inaccurate javascript timers. I guess it would have more sense with 1000ms interval for example. I have to deal with not-synced-fixed-timestep another way.

mrchimp commented 8 years ago

I'm gonna close this. If you still want some changes, holla!