Rishi N
Rishi N

Reputation: 67

How to use setInterval to trigger a function so that I can stop it at some point?

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;
}

Upvotes: 2

Views: 4527

Answers (7)

eis
eis

Reputation: 53553

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);

Upvotes: 2

Aayush Mall
Aayush Mall

Reputation: 1013

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;
}

Upvotes: 4

SandyKrish
SandyKrish

Reputation: 232

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");
}

Upvotes: 1

Mario Vernari
Mario Vernari

Reputation: 7306

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);    

Upvotes: 1

Thallius
Thallius

Reputation: 2619

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)
}

Upvotes: 0

Rahul Jain
Rahul Jain

Reputation: 357

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

var timerId = setInterval(func, 500);
.... some code here....
clearInterval(timerId);

Upvotes: 2

Ajay Bains
Ajay Bains

Reputation: 39

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

Upvotes: -1

Related Questions