My
setInterval('name(var)',6000);
won't clear with:
clearInterval('name(var)');
or with:
clearInterval('name(var)');
why?
setInterval returns a handle of the interval, so you do it like this:
var myInterval = setInterval(function(){ name(var) }, 6000);
clearInterval(myInterval);
Note: Notice how I used a anonymous function instead of a string too.
You need to store the returned intervalID and pass it to clearInterval later.
var intervalID = setInterval('name(var)',6000);
clearInterval(intervalID);
You need to pass in the interval id returned from setInterval
ex:
var id = setInterval('name(var)', 2000);
clearInterval(id);
More information here.
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/
This will work:
var id = setInterval('name(var)',6000);
clearInterval(id);
Related
this is the code
var t = ()=>{
setInterval(()=>{
console.log('hello')
},1000)
}
t();
clearInterval(t)
Why the clearinterval does not block execution of the setInterval?
It doesn't work on a function because that's just now how the mechanism was designed. Calls to setInterval() return a number that acts as an identifier for the timer that the call establishes. That number is what has to be passed to clearInterval().
It doesn't cause an error to pass something that's not a number, or to pass a number that doesn't identify an active timer, but the call has no effect.
In your case, your t() function could simply return the result of the setInterval() call, and your outer code can save that for use later however you like.
It's because you need to return the id of the interval and clear that id.
According to the documentation:
setInterval returns an interval ID which uniquely identifies the
interval, so you can remove it later by calling clearInterval().
//function that keeps logging 'hello' on the console
var t = ()=>{
//return the id of the interval
return setInterval(()=>{
console.log('hello')
},1000)
}
//get the id of the interval created by the "t" function
var id = t();
//alerts the id just to see it
alert(id);
//clear it - function will stop executing
clearInterval(id);
References
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval
because you should clearInterval on reference for setInterval().
var interval = setInterval();
clearInterval(interval);
T is not equal to the setInterval returned value as you don't return a value from your arrow function, and don't assign it to a value.
Try this snippet instead:
var t = ()=>
setInterval(()=>{
console.log('hello')
},1000)
var interval = t();
clearInterval(interval);
let intervalId = null;
cycle(true);
function cycle(r) {
let myInterval = () => {
return setInterval(() => plusSlides(1), 1000);
}
if (!r) {
clearInterval(intervalId);
} else {
intervalId = myInterval();
}
}
I have next code:
var timer = setInterval(function() {}, 1000)
When I try to output timer, it contains "2" (number).
Plunker: http://plnkr.co/edit/x0Iwc0ZjTI1HDFv9oxyc?p=preview
Please, explain me this behavior.
That's the interval ID. That's a number generated by the browser that you can reference later.
The reason for this is so you can use clearInterval to stop looping.
var timer = setInterval(function() {
// Will only run once
clearInterval(timer);
}, 1000);
I have the following function that performs a setTimeout back on itself, i.e.:
var doIt = function(){
$('#comment_report').trigger('apexrefresh');
setTimeout("doIt()", 5000);
}
My question is, I am trying to clear the setTimeout that is looping through every 5 seconds, by issuing:
clearTimeout(doIt);
Unfortunately this didn’t work.
How would I go about clearing the above setTimeout?
You don't pass a string to clearTimeout(), you pass the Number which identifies the setTimeout() (which is returned by said function).
var timeoutId = null;
var doIt = function() {
$('#comment_report').trigger('apexrefresh');
timeoutId = setTimeout(doIt, 5000);
};
clearTimeout(timeoutId);
Alternatively, inside of doIt(), check for the condition and if it's met, then return only before you issue another call to setTimeout() (you won't have to keep track of the id that way).
var doit = function(){
};
var my_timeout_name = setTimeout( doit, 5000 );
clearTimeout( my_timeout_name );
setTimeout returns you an id that you can then pass to clearTimeout function.
Also, instead of this:
setTimeout("doIt()", 5000);
You can just pass a function:
setTimeout(doIt, 5000);
var myTimeout = false;
var doIt = function(){
$('#comment_report').trigger('apexrefresh');
myTimeout = setTimeout(doIt, 5000);
}
Then you should use
clearTimeout(myTimeout);
You are doing it wrong clearTimeout(timerid) this function takes the id of Timeout.
you can achieve this as
var mytimerid;
var doIt = function()
{
$('#comment_report').trigger('apexrefresh');
mytimerid = setTimeout("doIt()", 5000);
}
and then when ever required stop the timeout by
clearTimeout(mytimerid);
You need to assign a variable to the setTimeout that holds the id.
But in your code you seem to be assigning doIt to a function that does not return anything.
var timer;
timer = setTimeout("doIt()", 5000);
Then use
if(timer)
clearTimeout(timer);
I have this function:
function timedFunction(functionString,timeoutPeriod) {
setTimeout(functionString+"timedFunction(\""+functionString+"\","+timeoutPeriod+");",timeoutPeriod);}
This function me call:
timedFunction("startStopky();",1000);
startStopky(); is a function that I want in a specified time interval repeatedly run. Everything works excellently, but if I want stop this interval, I have to stop as follows:
for (var i = 1; i < 99999; i++) {
window.clearInterval(i);
}
Unfortunately this will stop all intervals, and I want to stop just one particular. How can I do it?
Instead of doing recursive calls to timedFunction just do:
var intervalId = setInterval(startStopky, 1000);
and to clear it just do:
clearInterval(intervalId);
The setTimeout function returns a timeout ID that you use with clearTimeout to remove that timeout. The same goes for intervals but in that case it's a setInterval and clearInterval combo.
E.g.:
var t = setTimeout(yourFunction, 1000);
clearTimeout(t);
var i = setInterval(yourFunction2, 500);
clearInterval(i);
You have a Timeout, but you are clearing an Interval. clearInterval clears intervals, not timeouts.
You want window.clearTimeout(timeoutId)
If you want to stop a single one, you use the processId of that interval.
window.clearTimeout("13");
You really shouldn't be using strings to do this:
function timedFunction(fn, interval) {
var timerHandle;
function runIt() {
fn();
timerHandle.id = setTimeout(runIt, interval);
}
return timerHandle = { id: setTimeout(runIt, interval) };
}
Then you can call it like this:
var handle = timedFunction(startStopky, 1000);
To stop the process:
clearTimeout(handle.id);
I am trying to update a variable every one second. For that reason I am using setTimeout. But it does not update the variable. It logs out 0 just once. Here is my code
var yes=0;
setTimeout(function () {
console.log(yes);
yes++;
}, 1000);
Use setInterval, to finish the repitition you have to clear the interval using clearInterval(yourInterval);
Live Demo
var yes=0;
yourInterval = setInterval(function () {
console.log(yes);
yes++;
}, 1000);
setTimeout does repeat itself only once, try setInterval so that for every second it increments yes with 1
but remember to clear the interval after sometime else you get on to a never ending loop
using clearInterval()
setTimeOut method is just called once after a specific timeout which in your case is 1000, try to use setInterval method instead
jsBin demo with setTimeout
If you really want to stick to setTimeout (I appreciate that ;) )
than just wrap all into a function and recall it inside it self like:
var yes=0;
(function loop(){
setTimeout(function () {
console.log(yes);
yes++;
loop(); // loop recall
}, 1000);
})();
Otherwise go for setInterval:
var yes=0;
function count(){
console.log(yes);
yes++;
}
setInterval(count, 1000);