I’m writing an HTML5 Game Development Javascript framework and I want to provide the user the difference in time between the last tick and the current one.
setInterval(tick, 16.6666666);
function tick() {
update();
draw();
}
That’s what I have, but I want to have:
while (true) {
/* Calculate delta time */
tick(dt);
}
function tick(dt) {
update(dt);
draw();
}
I tried that, using date.getTime(); to calculate delta time, but Firefox said the script crashed. Obviously, an infinite loop will crash. Got any suggestions for how I can go about this?
I want an infinite loop, that can be stopped with break. I want to pass delta time too, but that I know how to do.
Just maintain a variable with the time of the last update, and calculate the elapsed time/delta time in
tickitself.Here’s a JSBin, though I don’t think it’s really needed… http://jsbin.com/okimam/10
EDIT:
I must point out that
setInterval(tick, 0)does not necessarily mean thattickwill be called immediately, with an interval of ‘0ms’ between two calls. It depends on the browser.