Cancel a Timer

function myTimer () {   alert("Timer Function Executed"); } var timerId = setTimeout(myTimer, 10000); clearTimeout(timerId); //timer will not execute

At any point before the time runs out and the code is executed, you can clear the timer by calling the clearTimeout(id) method using the ID returned from setTimeout(). Once the timer has been cleared, it does not execute. You need to keep track of the ID returned from setTimeout() using a variable.

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.