Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #21410

Re: Draw/animation fail to stop

Message-ID <2797316.uecmfydTqv@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-09-22 12:42 +0200
Subject Re: Draw/animation fail to stop
Newsgroups comp.lang.javascript
References <7f279d3b-dab8-4021-86f4-c504200547b3@googlegroups.com> <XnsA242A16BD8F17eejj99@194.109.133.133> <434079c0-5e5c-4aa0-be66-0ac5ec77e50e@googlegroups.com> <l1lspu$ckd$1@news.albasani.net>

Show all headers | View raw


Stefan Weiss wrote:

> On 2013-09-22 05:14, jonas.thornvall@gmail.com wrote:
>> Den lördagen den 21:e september 2013 kl. 15:52:06 UTC+2 skrev Evertjan.:
>>> jonas.thornval wrote on 21 sep 2013 in comp.lang.javascript:
>>> 
>>> > function initanime()
>>> > {
>>> >    size = 0;
>>> >    start = 0;
>>> >    multip = + 1;
>>> >    run = 1;
>>> >    refresh = setInterval(draw, 0);
>>> […]
>> 2 an interval of 0 ms is not an interval.
> 
> It's not? Next you're going to tell me 0 is not a percentage.
> http://tinyurl.com/zeroperc

Sarcasm aside, it is unwise to use 0 (milliseconds) for the second argument 
of window.setInterval(…) (which should be written so).  Normalization to a 
minimum sensible value may or may not occur.  If it does not occur, there 
will be high CPU load slowing down the browser and other applications 
because operations cannot be performed fast enough and intervals will 
overlap.  This has caused performance problems with Internet Explorer.

A value of 50 (ms) has proven to be a sensible minimum value for the 
majority of client applications, however the minimum normalization value is 
10 (ms) in Mozilla for 32-bit systems.

That said, window.setInterval() should be avoided in the first place (at 
least for short interval lengths, like for animations and transitions, 
should that still be considered necessary in view of CSS3) in favor of 
window.setTimeout() which is passed a function that calls 
window.setTimeout() again:

  var t = null;
  var f = function () {
    if (t !== null)
    {
      window.clearTimeout(t);
      t = null;
    }

    /* do something every 50 milliseconds */

    t = window.setTimeout(f, 50);
  };

  f();

The advantage of the latter is that timeouts do not overlap if a function 
call takes longer than the interval length.  This reduces CPU load as there 
will be only one timeout-triggered function call processed at the same time 
(in the same tab/browser).

I have posted a breakdown of this, with a reference to Mozilla code as an 
example, a while ago; you may still find it by searching for “setTimeout”, 
“setInterval”, and my (nick)name.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Draw/animation fail to stop jonas.thornvall@gmail.com - 2013-09-21 06:33 -0700
  Re: Draw/animation fail to stop "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-21 15:52 +0200
    Re: Draw/animation fail to stop jonas.thornvall@gmail.com - 2013-09-21 20:14 -0700
      Re: Draw/animation fail to stop Stefan Weiss <krewecherl@gmail.com> - 2013-09-22 06:47 +0200
        Re: Draw/animation fail to stop JJ <duh@nah.meh> - 2013-09-22 13:05 +0700
        Re: Draw/animation fail to stop "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-09-22 09:10 +0200
        Re: Draw/animation fail to stop jonas.thornvall@gmail.com - 2013-09-22 01:30 -0700
          Re: Draw/animation fail to stop jonas.thornvall@gmail.com - 2013-09-22 01:40 -0700
        Re: Draw/animation fail to stop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-22 12:42 +0200
          Re: Draw/animation fail to stop Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-09-23 18:06 +0100
            Re: Draw/animation fail to stop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-24 01:00 +0200
              Re: Draw/animation fail to stop Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-09-25 17:18 +0100
                Re: Draw/animation fail to stop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-26 00:55 +0200
                Re: Draw/animation fail to stop Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-09-25 22:33 -0300
                Re: Draw/animation fail to stop Andrew Poulos <ap_prog@hotmail.com> - 2013-09-26 15:21 +1000
                Re: Draw/animation fail to stop Dr J R Stockton <reply1300@merlyn.demon.co.uk.invalid> - 2013-09-28 19:16 +0100
      Re: Draw/animation fail to stop Christoph Michael Becker <cmbecker69@arcor.de> - 2013-09-22 14:14 +0200
        Re: Draw/animation fail to stop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-09-22 15:06 +0200
  Re: Draw/animation fail to stop Ken <usenet-so@abdiassoftware.com> - 2013-09-22 13:44 -0500
  Re: Draw/animation fail to stop Ken <usenet-so@abdiassoftware.com> - 2013-09-22 13:50 -0500

csiph-web