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


Groups > comp.lang.javascript > #33621

Re: timeline

Path csiph.com!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.32.229.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: timeline
Date Sun, 23 Jul 2017 18:57:21 +0200
Organization PointedEars Software (PES)
Lines 71
Message-ID <11824948.uLZWGnKmhe@PointedEars.de> (permalink)
References <D_6dndl-zu23-u3EnZ2dnUU7-V3NnZ2d@westnet.com.au>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
Injection-Info gwaiyur.mb-net.net; posting-host="32.229.197.178.dynamic.wless.zhbmb00p-cgnat.res.cust.swisscom.ch:178.197.229.32"; logging-data="8505"; mail-complaints-to="abuse@open-news-network.org"
User-Agent KNode/4.14.2
Cancel-Lock sha1:QOeUcvEF177BDj53fMvzQUeC23A=
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
X-User-ID U2FsdGVkX19v5TAxDhe+UlxZHAFQhzvnwHKlgtjulRfQTV3Q/DHPxw==
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Xref csiph.com comp.lang.javascript:33621

Show key headers only | 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