Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-10-2013, 02:39 PM   PM User | #1
nikos101
Senior Coder

 
nikos101's Avatar
 
Join Date: Dec 2006
Location: London
Posts: 1,004
Thanks: 58
Thanked 10 Times in 10 Posts
nikos101 is an unknown quantity at this point
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);

});
__________________

nikos101 is offline   Reply With Quote
Old 01-10-2013, 10:10 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-11-2013, 01:31 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
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"...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 01-11-2013, 08:10 PM   PM User | #4
hdewantara
Regular Coder

 
hdewantara's Avatar
 
Join Date: Aug 2009
Location: Jakarta, Indonesia.
Posts: 287
Thanks: 4
Thanked 39 Times in 39 Posts
hdewantara is an unknown quantity at this point
I hope this "self-adjusting" routine example could help you a bit
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
    <
meta content="text/html; charset=utf-8" http-equiv="content-type" />
    <
script type="text/javascript">
        var 
            
delayTarget 15,
            
delaySet delayTarget,
            
delaySetMin 10,
            
timeLast = new Date(),
            
timer setTimeout(rundelaySet);
            
        function 
run(){
            
//some procedures here
            
var spanText document.getElementsByTagName('span')[0].childNodes[0];
            
spanText.nodeValue 
                (
spanText.nodeValue == '/') ? '―' 
                    
: (spanText.nodeValue == '―') ? '\\' 
                        
: (spanText.nodeValue == '\\') ? '|' '/';
            
            
//report
            
var 
                
timeNow = new Date(),
                
delay timeNow.getTime() - timeLast.getTime();
            
document.getElementById('delay').value delay;
            
document.getElementById('delaySet').value delaySet;

            
//if was too slow or too fast
            
if (delay delayTarget
                
delaySet--;
            else 
                if (
delay delayTargetdelaySet++;
            if (
delaySet delaySetMin)
                
alert('Optimize procedure for faster execution, or set a longer target delay.');
            else{
                
//re-run
                
timeLast.setTime(timeNow.getTime());
                
timer setTimeout(rundelaySet);
            }
        }
    
</script>
</head>
<body>
    <label for="delay">Actual Delay:</label><input type="text" id="delay" readonly><br/>
    <label for="delaySet">Delay Set:</label><input type="text" id="delaySet" readonly>
    <span>/</span>
</body>
</html> 

Last edited by hdewantara; 01-11-2013 at 08:14 PM..
hdewantara is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 AM.


Advertisement
Log in to turn off these ads.