2

So from what I have understood, setInterval() is used to call a function on repeat on regular intervals. So basically it is a loop that executes a function forever periodically.

I am confused as to if I had to stop this execution at one point what would be the way to do it

for eg I am trying to print the message "hey" 3 times after 1 second each, but somehow it is printing it 3 times every second and is going on forever. What can I do to stop it after a set number of times.

This is the code that I've been trying

var i = 3;
function message() {
  console.log("hey");
}

while(i > 0) {
  setInterval(message, 1000);
  i = i - 1;
}
1
  • 1
    setInterval is not intended to ran x times, but rather to run every x ms. If you want it to run x times, see @eis 's answer how it can be accomplished using setTimeout instead.
    – Nir Alfasi
    Commented May 30, 2021 at 8:22

7 Answers 7

3

Your code is executing the setInterval thrice in the while loop, which is not needed. Actually, setInterval does not work as a function call but actually registers a function to be called at some interval. The setInterval() method will continue calling the function until clearInterval() i.e it is deregistered or the process is killed.

It should work like this

var i = 3;
var interval = setInterval(message, 1000);

function message() {
    if (i === 0) {
        clearInterval(interval);
    }

    console.log("hey");
    i = i - 1;
}
1
  • if (i-- === 0) or even just if (!i--)
    – OrangeDog
    Commented May 30, 2021 at 8:32
2

To clear a setInterval, use global clearInterval method. Example:

var timerId = setInterval(func, 500);
.... some code here....
clearInterval(timerId);
2
  • 2
    With the structure shown here, the interval will be cleared before it has ever run.
    – OrangeDog
    Commented May 30, 2021 at 8:30
  • This was just to keep the simplicity. Otherwise the place of clearInterval depends on use case. ... some code here... is not here to indicate that some code is running in sequence, it is there just to indicate that there you got some code and when you want to clear interval, this is what you need to use.
    – Rahul Jain
    Commented May 31, 2021 at 13:18
2

What can I do to stop it after a set number of times.

usually you don't use setInterval() for this, you use setTimeout().

Something like

var counter = 0;

function message() {
  console.log("hey");

  // we trigger the function again after a second, if not already done 3 times
  if (counter < 3) {
    setTimeout(message, 1000);
  }
  counter++;
}

// initial startup after a second, could be faster too
setTimeout(message, 1000);
1

The setInterval function calls the function indefinitely, whereas setTimeout calls the function once only.

Simply use clearInterval once the count runs out.

var i = 3;

function message(){
  console.log("hey");
  if (--i < 0) {
    clearInterval(tmr);
  }
}

var tmr = setInterval(message, 1000);    
1

you have to assign that setInterval to a javascript variable to name it what for this setInterval, like this

var messageLog = setInterval(message, 1000);

After, in setInterval message function add this condition to clear the inverval whenever you want to clear.

function message(){
  if(i>3) {
     clearInterval(messageLog);     // clearInterval is a javascript function to clear Intervals. 
     return null;
  }
   console.log("hey");
}
3
  • 1
    You need to increment i
    – OrangeDog
    Commented May 30, 2021 at 8:33
  • @OrangeDog no need, In his code he will increment or decrement inside the while loop, that's why am not adding inside message function.
    – SandyKrish
    Commented May 30, 2021 at 8:41
  • 1
    That's completely wrong. There shouldn't even be a while loop.
    – OrangeDog
    Commented May 30, 2021 at 8:56
0

You can retrieve the timer when creating and clear it if needed.

var i=3;
var timer = setInterval(message,1000);
function message(){
  console.log("hey");
  i—-;
  if(i==0)
      clearInterval(timer)
}
-1

a beginner here too,look for clearInterval method ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.