xxxxxxxxxx
// Set a timeout of 5 seconds
const timeout = setTimeout(() => {
console.log("Timeout completed");
}, 5000);
// Reset the timeout to 3 seconds
function resetTimeout() {
clearTimeout(timeout);
timeout = setTimeout(() => {
console.log("Timeout completed");
}, 3000);
}
// Call the resetTimeout function to reset the timeout
resetTimeout();
xxxxxxxxxx
var myVar;
function myFunction() {
myVar = setTimeout(function(){ alert("Hello"); }, 3000);
}
function myStopFunction() {
clearTimeout(myVar);
}
xxxxxxxxxx
// Get a reference to the timeout and call wwindow.clearTimeout() on
// that refrence.
let timeoutHandle = window.setTimeout( );
window.clearTimeout(timeoutHandle);
xxxxxxxxxx
// Set a timeout
const timeoutId = setTimeout(() => {
console.log('Timeout executed');
}, 5000); // 5000ms (5 seconds)
// Later, if you want to clear the timeout:
clearTimeout(timeoutId);
xxxxxxxxxx
var timer;
function endAndStartTimer() {
window.clearTimeout(timer);
var millisecBeforeRedirect = 10000;
timer = window.setTimeout(function() {
alert('Hello!');
}, millisecBeforeRedirect);
}
xxxxxxxxxx
The clearTimeout() method of the WindowOrWorkerGlobalScope mixin cancels a timeout previously established by calling setTimeout().