![]() |
The setInterval timing is not consistent across browsers.
Hi, I'm finding that the he elapsed number which is determined by the setInterval function is not consistent across browsers. The value can vary considerably. Can anyone advise on a way to get it more uniform.
update(elapsed: number) { var elapsedUnit = elapsed / 10; this code is called based on this setInterval code : $(document).ready(function () { var game: Game = new Game; $(document).keydown(game.onKeyDown); $(document).keyup(game.onKeyUp); //game.update(); //game.draw(); var timeA = new Date().getTime(); setInterval(function () { var timeB = new Date().getTime(); var elapsedTime = timeB - timeA game.update(elapsedTime); game.draw(); }, 1000 / this.FPS); }); |
setInterval adds the code to the queue to be run each time the specified number of miilliseconds has passed. The only reason why it would be delayed from running straight away once added would be if there is other JavaScript still running.
If you are getting significantly different results from this call in different browsers then you must be running a huge amount of JavaScript so that the queued code has to wait before being able to run - then how soon it runs would depend on how quickly the browser runs JavaScript. The only two ways to get it more uniform are to either run a lot less JavaScript in the page or to increase the number of milliseconds between executions. |
you would be better off using setTimeout() or requestAnimationFrame() so that your work loop cannot get backed up.
if something takes more than the frame display period, the next interval can get behind schedule. As these lagging executions stack up, the stack grows larger and memory consumption goes up. Some browsers throw away the backlog (chrome), while others simply grind to a halt (IE7). in a game loop, you want to fire as often as possible, not on a pre-determined schedule: nobody will complain that your game has "too many FPS"... |
I hope this "self-adjusting" routine example could help you a bit :)
PHP Code:
|
| All times are GMT +1. The time now is 02:27 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.