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
Why Are Inline Event Handler Attributes a Bad Idea in Modern Semantic Html
Strip HTML from Text JavaScript
Using Html5/Canvas/JavaScript to Take In-Browser Screenshots
Unescape HTML Entities in JavaScript
How to Prevent Form from Being Submitted
How to Send Formdata Objects With Ajax-Requests in Jquery
Jquery/JavaScript to Replace Broken Images
How to Append to Innerhtml Without Destroying Descendants' Event Listeners
Why Split the ≪Script≫ Tag When Writing It With Document.Write()
How to Load Up CSS Files Using JavaScript
How to Detect If JavaScript Is Disabled
How to Prevent Buttons from Submitting Forms
Difference Between Client-Side and Server-side Programming