Timer in firefox, opera, et al.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy

    Timer in firefox, opera, et al.

    Hello,
    I'm using the setTimeout function to perform some animation. The timeout
    I'm using is 30ms, which works well in IE and produces smooth animation.
    However, in other, more standards compliant browsers, it plays back much
    slower. It seems like this happens to the same degree on all of these
    browsers, and if I increase or decrease the timeout by a little bit it
    doesn't have any effect. This leads me to believe that there is some
    minimum timeout for this function in the standards (are there any
    standards?) but I can't find any info on this.

    If I increase the timeout and increase the "jump" of the animation, it works
    at the correct rate but looks like crap. Any way to get the other browsers
    to give me 30ms precision?

    Thanks,
    -Jeremy


  • Lasse Reichstein Nielsen

    #2
    Re: Timer in firefox, opera, et al.

    "Jeremy" <jeremys@uci.ed u> writes:
    [color=blue]
    > This leads me to believe that there is some minimum timeout for this
    > function in the standards (are there any standards?)[/color]

    There are no standards. There is a, browser and OS dependent, minimum
    delay, the timer granularity. However, in IE 6, Moz FF and Opera 7,
    that seems to be 10ms (as witnessed by this program:)
    ---
    var t1;
    var times = [];
    var n=0;
    function timer() {
    var t2 = new Date();
    times.push(t2-t1);
    t1=t2;
    if (--n>0) {
    setTimeout(time r,1);
    }
    }

    function report() {
    alert(times);
    }
    setTimeout(repo rt,1000);
    n=5;
    t1=new Date();
    setTimeout(time r,1);
    ---

    If the delay between rounds is more than that, maybe it is the time it
    takes to update the animation that counts against you. If you call the
    setTimeout again at the end of each step, you will be delayed by the
    time it takes to do the step.
    [color=blue]
    > If I increase the timeout and increase the "jump" of the animation, it works
    > at the correct rate but looks like crap. Any way to get the other browsers
    > to give me 30ms precision?[/color]

    I would use setInterval(ste p,30) and hope the steps take less than 30
    ms. It won't be exact, but it should be close enough.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: Timer in firefox, opera, et al.

      JRS: In article <lL1gc.9178$yD1 .28188@attbi_s5 4>, seen in
      news:comp.lang. javascript, Jeremy <jeremys@uci.ed u> posted at Sat, 17
      Apr 2004 03:45:21 :
      [color=blue]
      >I'm using the setTimeout function to perform some animation. The timeout
      >I'm using is 30ms, which works well in IE and produces smooth animation.
      >However, in other, more standards compliant browsers, it plays back much
      >slower. It seems like this happens to the same degree on all of these
      >browsers, and if I increase or decrease the timeout by a little bit it
      >doesn't have any effect. This leads me to believe that there is some
      >minimum timeout for this function in the standards (are there any
      >standards?) but I can't find any info on this.
      >
      >If I increase the timeout and increase the "jump" of the animation, it works
      >at the correct rate but looks like crap. Any way to get the other browsers
      >to give me 30ms precision?[/color]

      You could have consulted the newsgroup FAQ on the subject of time; see
      sig below.

      The resolution of the date object itself is by definition one
      millisecond; but the values of new Date() increase in steps of 10 ms for
      WinNT+ systems or 55 ms for Win98x systems (and possibly other values
      for other systems).

      <URL:http://www.merlyn.demo n.co.uk/js-dates.htm#OV> refers; and will
      show the resolution of the browser being used.

      The same timing mechanism is (extremely likely to be) used for the
      setTimeout function, and for setInterval too.

      I expect the resolution to be an OS property, otherwise independent of
      browser version.


      For use on a 55 ms system, you could in principle write a time-wasting
      loop, see how many times it could be executed between successive 55 ms
      timeouts, similarly time your display routine, then use that information
      to attempt a hand-crafted 27.5 ms interval between a timed display call
      and a fill-in one. It seems a dubious approach, but there may be no
      better.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr times, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

      Comment

      Working...