How do you stop a setInterval function?

Hi! I’ve been doing the 25 + 5 Clock Project since a couple days but there is a problem with which i’m struggling to that is when the reset button is clicked every timer should be stopped and everything should be returned to its initial state…

So, i put the setInterval function inside a function that is called timer() in react but now i want to make another function called reset that stops the timer() function but i can’t call clearInterval outside that function, what can i do? help…!! Here is my code…!


To stop it, you call clearInterval with the result of the call to setInterval:

const handle = setInterval(function() {
  console.log('Printing every 5 seconds');
}, 5000);

const clearHandle = setInterval(function() {
}, 6000);
// This should run once

Thank you for your feedback! I will try it!

1 Like

I appreciate that you’ve checked my code, and reply a possible solution, i try it and it didn’t work, probably because it’s react and the things are a little bit more complicated, but i put the set interval function inside the state and i could clear it inside the reset function and is working as i want! Thank you though!
Here’s my edited code

1 Like