javascript setInterval is slow in ie8 -
1) chrome, firefox fine slow in ie8
2) slow after browser zoom in/out, resize, etc
3) why? memory leak? or ?
function movex(object, coord){ var v = (coord < 0) ? -1 : 1; var timer = setinterval(function(){ var x = object.offsetleft + v; v *= 2; if((v < 0 && x > coord) || (v > 0 && x < coord)) object.style.left = x + "px"; else{ clearinterval(timer); object.style.left = coord + "px"; } }, 25); }
first off, mentioned in comments, make sure multiple timers aren’t running @ same time. next, not forcing offsetleft
calculated each time offer quite speedup.
function movex(object, coord) { var v = coord < 0 ? -1 : 1; var x = object.offsetleft; var timer = setinterval(function () { x += v; v *= 2; if ((v < 0 && x > coord) || (v > 0 && x < coord)) { object.style.left = x + "px"; } else { clearinterval(timer); object.style.left = coord + "px"; } }, 25); }
also, motion dependant on how long takes timer tick; performance differences might able smoothed on using time exclusively (though can’t correctly in ie 8; there’s no monotonic timer api):
function movex(object, coord) { var v = coord < 0 ? -1 : 1; var startx = object.offsetleft; var start = new date().gettime(); var timer = setinterval(function () { var t = new date() - start; var x = startx + v * math.pow(2, t / 25); if ((v < 0 && x > coord) || (v > 0 && x < coord)) { object.style.left = x + "px"; } else { clearinterval(timer); object.style.left = coord + "px"; } }, 25); }
that come @ cost of real performance, though.
Comments
Post a Comment