The clearTimeout() method cancels the execution of the function set in setTimeout().
| window.clearTimeout(timeoutVariable) |
The clearTimeout() method can be used without the window prefix.
It takes as an argument the variable returned by setTimeout().
| myVar = setTimeout(function, milliseconds); clearTimeout(myVar); |
If the function has not yet executed, you can prevent its execution by calling the clearTimeout() method.
The same example as above, but with an additional “Stop” button.
| <button onclick=”myVar = setTimeout(myFunction, 3000)”>Try it</button> <button onclick=”clearTimeout(myVar)”>Stop it</button> |