0

I need to run a setInterva () but when trying to do it within a while cycle the setInterval() never runs. I'm a beginner in javascript so I don't know what the reason is.

The code I try to execute is the following:

var i = 0;

while(i < 5){

    var c = 0;

    var t = setInterval(function(){
      c++;
      if(c==5){
        clearInterval(t);   
      }
     },2000);

    i++;
}

What is the method to create a time.sleep() within a while cycle in javascript?

Jhonatan Zu
  • 159
  • 2
  • 7
  • 1
    Code makes no sense. You are looping to create 5 different intervals, which are loops themselves. Once the first one gets to 5 the rest will clear as well. What are you really trying to do? – StackSlave Nov 05 '19 at 00:40
  • 1
    What are you really trying to do? You're starting 5 interval timers, but you're overwriting `t` each time. So when you do `clearInterval(t)` it only clears the last one. They're also all incrementing the same variable `c`. – Barmar Nov 05 '19 at 00:40
  • 1
    You should either be using `setTimeout` to create one timer each iteration, or use `setInterval` once and have it clear itself when the counter reaches 5. – Barmar Nov 05 '19 at 00:41
  • 1
    Why do you think `setInterval()` never runs? The function doesn't do anything visible. – Barmar Nov 05 '19 at 00:42
  • Hello friends, I explain, I come from the python language and in that language there is the function time.sleep () that is used to sleep a while loop for a certain time. I only try to do the equivalent of that in javascript and setInterval () is the closest thing I found in javascript. How can I make a cycle wait 5 seconds in javascript – Jhonatan Zu Nov 05 '19 at 00:50
  • 2
    The interval will run in that code however it will only run once since all variables are overwritten and the first 4 have no chance of finishing. You also cannot tell if the last interval runs or not because there is no logs to console or anything confirming it, the fact that a runtime error doesnt occur is your best evidence. For what you have said in your last comment, to make a cycle every 5 seconds in Javascript, you just need one setInterval call, e.g. `setInterval(function() { console.log("test"); }, 5 * 1000);` (Units are in milliseconds) – Sean Nov 05 '19 at 01:48

1 Answers1

1

As mentioned in the comments above, setInterval() does not pause code execution. Instead everything else runs as normal, but after the set amount of time it runs the function. Also, you want to use setTimeout because it only runs once. It looks like you just want to increment c by one every five seconds. Do so like this:

let i = 0;
let c = 0;

while (i < 5) {
    setTimeout(() => c += 1, 5000/*number of milliseconds you want to wait*/ * (i + 1))
}

so, i will increment after 5000 milliseconds, 10000 milliseconds, 15000, etc.

Just so you know: () => i+= 1 is the same as

function() {
    i += 1
}

With some refactoring, and using for loops, your code becomes:

let c = 0
for(let k = 0; k < 5; k += 1) setTimeout(() => c += 1, 5000 * (k + 1))
MilesZew
  • 442
  • 1
  • 7
  • 18