T
T
targyn2021-06-15 14:20:31
JavaScript
targyn, 2021-06-15 14:20:31

How to stop the for loop and then continue from the step where you left off?

I need to implement a For loop when a button is clicked, take into account the conditions that the loop should take a step and stop. When you click on the same button, continue the cycle from the step where it stopped.
The if option with a counter is not suitable, I need it with the for loop.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-06-15
@targyn

Generator (except IE).

function* generator() {
  for (let i = 0; i < 2; i += 1) {
    yield i;
  }
}

const gen = generator();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // undefined

https://developer.mozilla.org/en/docs/Web/JavaScript...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question