最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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

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;
}
Share Improve this question edited May 30, 2021 at 8:16 eis 53.6k14 gold badges157 silver badges203 bronze badges asked May 30, 2021 at 8:02 Rishi NRishi N 671 silver badge7 bronze badges 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 acplished using setTimeout instead. – Nir Alfasi Commented May 30, 2021 at 8:22
Add a ment  | 

7 Answers 7

Reset to default 4

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

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

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

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

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论