2

I want to run myCustomFunction inside "Console of Google Chrome" with 1 second delay on each loop.

Tried setInterval but no avail as no execution delay was observed inside Console of Google Chrome.

const  myCustomFunction = i => console.log('iteration test number', i);
for (let i = 0; i < 11; i++) {
    setInterval(myCustomFunction(i), 1000);
};
  1. I expect Google Chrome's Console to delay 1 second (or more) for each iteration before running myCustomFunction.
  2. I need this to work inside the Console of Google Chrome, not module in 'fileName.js'
bg27
  • 47
  • 1
  • 5
  • 1
    Possible duplicate of [How do I add a delay in a JavaScript loop?](https://stackoverflow.com/questions/3583724/how-do-i-add-a-delay-in-a-javascript-loop) – Devsi Odedra Feb 16 '19 at 06:14

4 Answers4

1

I have modified your code to give expected output.

const  myCustomFunction = i => console.log('iteration test number', i);
for (let i = 0; i < 11; i++) {
    setTimeout(myCustomFunction, 1000 * i, i);
}

Let us discuss changes,

  1. Replaced setInterval with setTimeout: setInterval executes given function on an given interval. So, if you called setInterval(myCustomFunction, 1000), it will repeatedly execute myCustomFunction after every 1s. This is not the behavior you want, you only want a delay of 1s and for that setTimeout is more appropriate.
  2. first argument of setInterval/setTimeout is a function, but output of myCustomFunction(i) was undefined. So, instead of calling myCustomFunction there just pass it.
  3. Changed delay from 1000 to i*1000: As contents of for loop executes without any delay. So, change delay to execute myCustomFunction after i seconds.
  4. 3rd parameter of setTimeout i: setTimeout passes all parameters after 2nd(delay) to callback function(in our case myCustomFunction).

For reference visit

  1. https://developer.mozilla.org/ro/docs/Web/API/window.setTimeout
  2. https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
goyaltushar92
  • 296
  • 1
  • 13
0

You can do as following, using setInterval as main function instead of loop.

const  myCustomFunction = i => console.log('iteration test number', i);
let iteration = 0;
const delay = 1000;
const tillCount = 11;

setInterval(() => {
    if (iteration < tillCount) {
      iteration ++;
      myCustomFunction(iteration);
    }
}, delay);
Mukundhan
  • 3,284
  • 23
  • 36
0

You need to use setInterval to execute your function (as many times you set) and stop it with clearInterval. Otherwise, your setInterval will keep running forever.

const customFunction = i => console.log('Iteration:', i);
let counter = 0;
const maxIteration = 5;
const delay = 1000;

const intervalId = setInterval(() => {
    if (counter < maxIteration) {
      counter++;
      customFunction(counter);
    } else {
      clearInterval(intervalId);
    }
}, delay);
Chris Tapay
  • 1,004
  • 10
  • 21
0

Se the code below and read the comment to understand

const  myCustomFunction = i => console.log('iteration test number', i);
repeat = 10; // how many time to repeat;
var i =0; // value to increase.
function loop(){
myCustomFunction(i); 
i++;
if (i<= repeat) // if the loop is suppose to continue
    setTimeout(loop, 1000); // wait 1s and loop agen
}

loop();
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31