Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear a javascript timeout thats set within a function

I have a recursive type function in Javascript that runs like this:

function loadThumb(thumb) {
    rotate=setTimeout(function() {
        loadThumb(next);
    }, delay);
}

Note: I've simplified the function to make it easier to read.

I have "a" tags called like this

<a href="#" onclick="loadThumb(3); clearTimeout(rotate);">Load thumb 3</a>

However, they don't clearout the timer, the timer continues to cycle through the function irregardless of the clearTimeout() being called.

Any ideas why? I think it might have something to do with a scope problem or something like that.

like image 689
Talon Avatar asked Jan 06 '12 08:01

Talon


People also ask

Can we clear setTimeout?

This will be the ID that was returned by the setTimeout() function. You can clear the timeout by using this id which identifies the timeout. The ids which will be sent as a parameter to this function are being shared by two functions setTimeout() and setInterval().

How do I clear timeout in React JS?

To clear or cancel a timer, you call the clearTimeout(); method, passing in the timer object that you created into clearTimeout(). For example, the code below shows how to properly clear a timer inside of a functional React component. const App = () => { useEffect(() => { const timer = setTimeout(() => console.

How do you stop time in JavaScript?

How to Stop the Execution? The clearTimeout() method stops the execution of the function specified in setTimeout(). The window.clearTimeout() method can be written without the window prefix.

What is the alternative for setTimeout in JavaScript?

The setInterval method has the same syntax as setTimeout : let timerId = setInterval(func|code, [delay], [arg1], [arg2], ...) All arguments have the same meaning. But unlike setTimeout it runs the function not only once, but regularly after the given interval of time.


3 Answers

Yeah, you need to make rotate a global variable. Simply declare it outside the function like so:

var rotate;
var delay = 1000;

function loadThumb(thumb) {
    alert("loading thumb: " + thumb);
    rotate = setTimeout(function() {
        loadThumb(thumb + 1);
    }, delay);
}

Also, you need to make sure you clear the timeout before you call loadThumb. Otherwise you'll clear the timer you just started.

<a href="#" onclick="clearTimeout(rotate); loadThumb(3);">Load thumb 3</a>

fiddle: http://jsfiddle.net/63FUD/

like image 92
benesch Avatar answered Sep 20 '22 13:09

benesch


it may be the issue of scope so make rotate as global variable and call clearTimeout(rotate);

refer clearTimeout() example

like image 37
Hemant Metalia Avatar answered Sep 19 '22 13:09

Hemant Metalia


It may be a scoping issue if you are not declaring rotate externally.

Try this:

var rotate = 0;
function loadThumb(thumb) {

    rotate=setTimeout(function() {
        loadThumb(next);
    }, delay);

}
like image 44
James Wiseman Avatar answered Sep 20 '22 13:09

James Wiseman