...

View Full Version : setTimeout



Akidi
05-23-2009, 07:30 PM
var j={
runTimer: function(){
var now = new Date().getTime();
var timeDelta = now-battle.lastGameCycleTime;
j.incrementTimers(now);

var cycleDelay = battle.gameCycleDelay;
if(timeDelta>cycleDelay)
cycleDelay = Math.max(1,cycleDelay-(timeDelta-cycleDelay));

battle.lastGameCycleTime = now;
if(battle.cont<5) { battle.timer[0] = setTimeout(j.runTimer,cycleDelay); }
},

incrementTimers: function(now){
var status=player.status.toLowerCase();
var pbat=player.battle;
if(pbat.atbInc===0) {
if(status==='haste') pbat.atbInc = Math.floor((126*(pbat.speed+20))/16);
else if(status==='slow') pbat.atbInc = Math.floor((48*(pbat.speed+20))/16);
else pbat.atbInc = Math.floor((96*(pbat.speed+20))/16);
$('content').appendChild(document.createElement('br'));
}
pbat.atb+=pbat.atbInc;
battle.timeCounter++;
if(pbat.atb>65535) { pbat.atb-=65536; battle.cont++; }
if(battle.timeCounter%16===0)
pbat.iTimer+=(status==='haste'?84:status==='slow'?32:64);
if(pbat.iTimer>255) {
pbat.iTimer-=256;
$('content').appendChild(document.createTextNode(new Date().getTime()-battle.date));
$('content').appendChild(document.createElement('br'));
battle.date=now;
}
}
};

battle = {
gameCycleDelay:1000/30,
lastGameCycleTime:0,
timeCounter:0,
cont:0,
timer:[],
date:0
};

player = {
status:'Haste', //'Haste','Slow' or 'Normal'
battle: {
iTimer:0,
speed:235,
atbInc:0
},
};

window.onload = function(){
j.runTimer();
};


running the above code on FF / Safari yields a run of ~1.5s, but in IE yields ~1.8s, higher delays with 'normal' status and lower speeds. Anyway to correct for this offset? Changing player.battle.speed to say 40, will show similar discrepancy of ~200-300ms. Any way to get rid of this without forking code a lot. I'd rather not use conditional comments if at all possible, as I would like to understand WHY this discrepancy occurs and code it out



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum