How do I stop a window.setInterval in javascript?
There's no built-in "pause" function, but you can stop it, and then start a new interval with the same function.
First, you need to capture the id returned by the call to setInterval
:
let intervalId = window.setInterval(...);
Then when you want to stop it, call
window.clearInterval(intervalId);
In your case I'd suggest defining setScreen
by itself, and not inside of the call to setInterval
. This way you can just use intervalId = window.setInterval(setScreen, 2000)
when you want to resume it.
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()
.
How can I pause setInterval() functions?
You could use a flag to keep track of the status:
var output = $('h1');var isPaused = false;var time = 0;var t = window.setInterval(function() { if(!isPaused) { time++; output.text("Seconds: " + time); }}, 1000);
//with jquery$('.pause').on('click', function(e) { e.preventDefault(); isPaused = true;});
$('.play').on('click', function(e) { e.preventDefault(); isPaused = false;});
h1 { font-family: Helvetica, Verdana, sans-serif; font-size: 12px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><h1>Seconds: 0</h1><button class="play">Play</button><button class="pause">Pause</button>
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 make a setInterval stop after some time or after a number of actions?
To stop it after running a set number of times, just add a counter to the interval, then when it reached that number clear it.
e.g.
var timesRun = 0;
var interval = setInterval(function(){
timesRun += 1;
if(timesRun === 60){
clearInterval(interval);
}
//do whatever here..
}, 2000);
If you want to stop it after a set time has passed (e.g. 1 minute) you can do:
var startTime = new Date().getTime();
var interval = setInterval(function(){
if(new Date().getTime() - startTime > 60000){
clearInterval(interval);
return;
}
//do whatever here..
}, 2000);
Pause document.ready setInterval with external function
You can try this
$( document ).ready(function() {
var timer = window.setInterval(myAction, 1000);
function pauseTimer() {
clearInterval(timer )
}
function playTimer() {
timer = window.setInterval(myAction, 1000);
}
function myAction(){
console.info('myAction')
}
$("#play").click(function(){
playTimer()
});
$("#pause").click(function(){
pauseTimer()
});
});
HTML
<div>
<button id="play" >play</button>
<button id="pause" >pause</button>
</div>
Related Topics
How to Upload a File Using Jquery.Ajax and Formdata
Positive Look Behind in JavaScript Regular Expression
A Proper Wrapper for Console.Log with Correct Line Number
Why Is Immutability So Important (Or Needed) in JavaScript
What Is "Callback Hell" and How and Why Does Rx Solve It
How to Check Whether <Ng-Content> Is Empty? (In Angular 2+ Till Now)
Jquery Difference Between Change and Click Event of Checkbox
Is a JavaScript Array Index a String or an Integer
Clearrect Function Doesn't Clear the Canvas
How to Filter JSON Data in JavaScript or Jquery
Node.Js Shell Command Execution
Break Promise Chain and Call a Function Based on the Step in the Chain Where It Is Broken (Rejected)
Safe Evaluation of Arithmetic Expressions in JavaScript
What Does the Function Then() Mean in JavaScript
Select All Contents of Textbox When It Receives Focus (Vanilla Js or Jquery)