Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #38951 > unrolled thread
| Started by | Diego Leonardo Robayo bernal <dr2882@columbia.edu> |
|---|---|
| First post | 2020-10-14 13:34 -0700 |
| Last post | 2020-10-16 12:38 -0700 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Diego Leonardo Robayo bernal <dr2882@columbia.edu> |
|---|---|
| Date | 2020-10-14 13:34 -0700 |
| Subject | How can I make “i” from a main loop reflect the times that an inner loop inside that main loop runs |
| Message-ID | <1660ec09-67d0-4224-b1b4-0a14023ae98dn@googlegroups.com> |
I am running a for loop that will call typeDelay(i) This function will be typing characters one by one on each iteration. Inside typeDelay(i), when the two first IF statement inside setTimeout are met (the length of the text that I am typing is equal to 52 and it is not an empty space), I want to know when I will hit an empty space by starting a loop that types characters, and stopping it when I get to an empty space. For this iteration I need to increase i so that each time I type the next character. The problem is that on this iteration I am going to be increasing the i that belongs to the typeDelay() function and not the one that belongs to the main loop that called typeDelay(i), which I need to do because every character that is added inside this inner loop does not need to be added from the main loop. When I go back to the original loop, the i belonging to this main loop is still going to have the value that it had before the two IF statement inside typeDelay(i) were met. And I want this i belonging to the main loop to also reflect the times that the inner loop inside typeDelay(i) ran. How can I make the i in the main loop to reflect the times that the loop inside typeDelay(i) ran. Find the code here: https://stackoverflow.com/questions/64357143/how-can-i-make-i-from-a-main-loop-reflect-the-times-that-an-inner-loop-inside
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2020-10-14 23:45 +0100 |
| Message-ID | <87y2k8qves.fsf@bsb.me.uk> |
| In reply to | #38951 |
Diego Leonardo Robayo bernal <dr2882@columbia.edu> writes: <cut> > Find the code here: > > https://stackoverflow.com/questions/64357143/how-can-i-make-i-from-a-main-loop-reflect-the-times-that-an-inner-loop-inside You'll get better answers if you post the code here. Try, also, to cut the example down. > How can I make the i in the main loop to reflect the times that the > loop inside typeDelay(i) ran. I think you have a bigger problem: the "main loop" is likely to have finished before any of the code triggered by setTimeout even runs. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | JJ <jj4public@gmail.com> |
|---|---|
| Date | 2020-10-15 11:26 +0700 |
| Message-ID | <1lns8aru8aunz.cydp3ckqozy1.dlg@40tude.net> |
| In reply to | #38951 |
On Wed, 14 Oct 2020 13:34:45 -0700 (PDT), 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. > > Find the code here: > > https://stackoverflow.com/questions/64357143/how-can-i-make-i-from-a-main-loop-reflect-the-times-that-an-inner-loop-inside Rather than creating multiple timers all at once, create only one timer which at its end of task, creates another timer that will call itself. Whether with the same time duration, or different one. That way, it guarantees that the timer function is called once per duration of time. Basically like using `setInterval()` but with modifiable time interval. Of course, the timer function will need to be adapted so that when it's called, it uses a different value or index from a variable - where that variable is updated at end of the timer function's task. i.e. so that the variable points to the next value or index.
[toc] | [prev] | [next] | [standalone]
| From | Julio Di Egidio <julio@diegidio.name> |
|---|---|
| Date | 2020-10-15 07:47 -0700 |
| Message-ID | <51e9f1b0-6d4f-446a-a4e9-d87c76e587b6o@googlegroups.com> |
| In reply to | #38951 |
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
[toc] | [prev] | [next] | [standalone]
| From | Diego Leonardo Robayo bernal <dr2882@columbia.edu> |
|---|---|
| Date | 2020-10-16 12:38 -0700 |
| Message-ID | <56c2b418-b2c1-4ed0-ba2b-32e960db07a1n@googlegroups.com> |
| In reply to | #38954 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web