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


Groups > comp.lang.javascript > #38963

Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs

Newsgroups comp.lang.javascript
Date 2020-10-16 12:38 -0700
References <1660ec09-67d0-4224-b1b4-0a14023ae98dn@googlegroups.com> <51e9f1b0-6d4f-446a-a4e9-d87c76e587b6o@googlegroups.com>
Message-ID <56c2b418-b2c1-4ed0-ba2b-32e960db07a1n@googlegroups.com> (permalink)
Subject Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs
From Diego Leonardo Robayo bernal <dr2882@columbia.edu>

Show all headers | View raw


On Thursday, October 15, 2020 at 10:47:17 AM UTC-4, ju...@diegidio.name wrote:
> On Wednesday, 14 October 2020 22:34:54 UTC+2, Diego Leonardo Robayo bernal wrote: 
> 
> > How can I make the i in the main loop to reflect the times that 
> > the loop inside typeDelay(i) ran.
> Nice, some non-trivial *event-driven programming*. 
> 
> Here is a starting point: 
> 
> JavaScript nested async loop (CodePen) 
> <https://codepen.io/jp-diegidio/pen/BazjOgb> 
> 
> (Of course it can be improved: in particular, 
> it does not support cancelling the iteration...) 
> 
> The gist of it boils down to this function: 
> 
> ---------------- 
> function runIter(onTick, onDone) { 
> _run(onTick, onDone); 
> 
> function _run(onTick, onDone) { 
> var count = 0; 
> 
> _iter(0, 0, onTick, function onIterDone(n, i) { 
> count += i; 
> 
> if (++n < RUN_COUNT) { 
> _iter(n, 0, onTick, onIterDone); 
> } else { 
> onDone(count); 
> } 
> }); 
> } 
> 
> function _iter(n, i, onTick, onIterDone) { 
> if (i < ITER_COUNT) { 
> onTick(n, i); 
> 
> _next(n, ++i, onTick, onIterDone); 
> } else { 
> onIterDone(n, i); 
> } 
> } 
> 
> function _next(n, i, onTick, onIterDone) { 
> window.setTimeout(function () { 
> _iter(n, i, onTick, onIterDone); 
> }, ITER_DELAY); 
> } 
> } 
> ---------------- 
> 
> Study it until you get it... :) 
> 
> HTH, 
> 
> Julio
Thanks Julio. It took me a while to understand the loop logic but I ended up doing something very similar 

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


Thread

How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs Diego Leonardo Robayo bernal <dr2882@columbia.edu> - 2020-10-14 13:34 -0700
  Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-10-14 23:45 +0100
  Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs JJ <jj4public@gmail.com> - 2020-10-15 11:26 +0700
  Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs Julio Di Egidio <julio@diegidio.name> - 2020-10-15 07:47 -0700
    Re: How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs Diego Leonardo Robayo bernal <dr2882@columbia.edu> - 2020-10-16 12:38 -0700

csiph-web