Stop Setinterval

Stop setInterval call in JavaScript

setInterval() returns an interval ID, which you can pass to clearInterval():

var refreshIntervalId = setInterval(fname, 10000);

/* later */
clearInterval(refreshIntervalId);

See the docs for setInterval() and clearInterval().

Stop setInterval

You need to set the return value of setInterval to a variable within the scope of the click handler, then use clearInterval() like this:

var interval = null;
$(document).on('ready',function(){
interval = setInterval(updateDiv,3000);
});

function updateDiv(){
$.ajax({
url: 'getContent.php',
success: function(data){
$('.square').html(data);
},
error: function(){
clearInterval(interval); // stop the interval
$.playSound('oneday.wav');
$('.square').html('<span style="color:red">Connection problems</span>');
}
});
}

How to exit from setInterval

Use clearInterval:

var refreshId = setInterval(function() {
var properID = CheckReload();
if (properID > 0) {
clearInterval(refreshId);
}
}, 10000);

how to stop setInterval in react native

i ran into a similar problem some months back, this soluion should work perfectly:

const myinterval = setInterval(function () {
checkCharge();
}, 10000);

but in my case, since I store the setInterval in a variable instead of a function, I had some weird problems, like the setInterval might run three times or keep on running even after I used clearInterval(INTERVAL_NAME); so if you want to check if a payment has been made, create a button that would tell the user to click when they have made the payment, this would be a lot safer than to run the function inside of setInterval

Stop setInterval function with a button

setInterval returns an ID that can be passed to clearInterval to stop it.

Example to stop an interval after some time:

let i = 0;
const intervalID = setInterval(() => console.log(i++), 100);
setTimeout(() => clearInterval(intervalID), 1000);

See also the examples at https://developer.mozilla.org/en-US/docs/Web/API/setInterval#examples.

How can I stop setInterval?

according to your comment seems like you need to stop the polling in react.
you can use the useRef hook to keep the reference of the interval and clear it whenever you like.

 useEffect(() => {
interval.current = setInterval(() => {
...
}, ms)

}, [input]);

const closeConnection = () => {
clearInterval(interval.current);

}

Stop setInterval() when Component Unmounts? (React)

You need to return a cleanup function from your effect:

useEffect(() => {
const updatePostInfo = setInterval(() => {
dispatch(getPostInfo(data._id));
}, 3500);
return () => clearInterval(updatePostInfo);
}, []);


Related Topics



Leave a reply



Submit