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


Groups > comp.lang.javascript > #33621

Re: timeline

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: timeline
Date 2017-07-23 18:57 +0200
Organization PointedEars Software (PES)
Message-ID <11824948.uLZWGnKmhe@PointedEars.de> (permalink)
References <D_6dndl-zu23-u3EnZ2dnUU7-V3NnZ2d@westnet.com.au>

Show all headers | View raw


Andrew Poulos wrote:

> var timeline = ["a", "b", "c",...];
> 
> where "a" "b" are the names of the functions to call and control code
> that looks like
> 
> var position = 0;
> 
> function runTimeline() {
>    position = (position == timeline.length ? position : 0);
>    window.timeline[position]();

Never use “window” to refer to the global object; pass a reference to it 
instead:

  var timeline = ["a", "b", "c",...];

  function runTimeline (global)
  {
    // …
    global.timeline[position]();
  }

  runTimeline(this);

That said, avoid global variables and functions, and referring to the global 
object, in the first place.

>    position++;
> }
> 
> function a() {
>    // do stuff and then when done
>    runTimeline();
> }
> function b() {
>    // do stuff and then when done
>    runTimeline();
> }
> 
> Is that a workable approach

I do not think so.  All of this still runs in a single thread, so you would 
be blocking the browser/tab.  Also, consider stack overflow:

  runTimeline()
    a()
      runTimeline()
        b()
          runTimeline()
            …
  
And I do not see how you would prevent items from being displayed earlier, 
before they are fully loaded, this way.

> or is there a better/smarter way?

As should be obvious now, *anything* is better/smarter than this.

ISTM that ultimately you will want to use the Promise pattern, triggering 
the next step only when the previous step has been completed *and* the 
required resources have been loaded (i.e. the Promise has been fulfilled).  
Built-in Promise support was just added in ECMAScript 2016, so you will 
probably have to use a framework (pre-existing or your own implementation) 
to make it work in all of your target environments.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
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

timeline Andrew Poulos <ap_prog@hotmail.com> - 2017-07-20 18:17 +1000
  Re: timeline "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-20 10:26 +0200
  Re: timeline Wally W. <ww84wa@aim.com> - 2017-07-20 08:37 -0400
    Re: timeline Andrew Poulos <ap_prog@hotmail.com> - 2017-07-21 07:47 +1000
      Re: timeline Wally W. <ww84wa@aim.com> - 2017-07-20 23:04 -0400
        Re: timeline Andrew Poulos <ap_prog@hotmail.com> - 2017-07-21 16:08 +1000
          Re: timeline "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-21 11:36 +0200
            Re: timeline Julio Di Egidio <julio@diegidio.name> - 2017-07-21 08:50 -0700
              Re: timeline "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-22 00:03 +0200
            Re: timeline Andrew Poulos <ap_prog@hotmail.com> - 2017-07-22 15:00 +1000
              Re: timeline "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-22 10:19 +0200
                Re: timeline Andrew Poulos <ap_prog@hotmail.com> - 2017-07-23 09:08 +1000
                Re: timeline "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2017-07-23 10:15 +0200
  Re: timeline "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-07-20 05:56 -0700
    Re: timeline Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-07-23 19:07 +0200
  Re: timeline Julio Di Egidio <julio@diegidio.name> - 2017-07-20 05:59 -0700
  Re: timeline Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-07-23 18:57 +0200
    Re: timeline Julio Di Egidio <julio@diegidio.name> - 2017-07-23 10:33 -0700

csiph-web