-2

How would I stop this loop? The loop is necessary, I'm just not sure how I stop it when I need to.

setInterval(function(){
        console.log("Loop running");
}, 6000);
Justin
  • 33
  • 4

2 Answers2

0

const interval = setInterval(...

clearInterval(interval);

Bojan Bedrač
  • 846
  • 1
  • 7
  • 10
0

You can use clearInterval(intervalId)

let intervalId  = setInterval(function(){
        console.log("Loop running");
}, 6000);

// on some action

clearInterval(intervalId);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60