Finestra azzeraTimeout()
Esempio 1
Come impedire a myGreating() di eseguire:
const myTimeout = setTimeout(myGreating, 3000);
function myGreating() {
document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}
function myStopFunction() {
clearTimeout(myTimeout);
}
Altri esempi di seguito.
Definizione e utilizzo
Il clearTimeout()
metodo cancella un timer impostato con il
setTimeout()
metodo.
Nota
Per cancellare un timeout, usa l' id restituito da setTimeout():
myTimeout = setTimeout(function, milliseconds);
Quindi puoi interrompere l'esecuzione chiamando clearTimeout():
clearTimeout(myTimeout);
Guarda anche:
Sintassi
clearTimeout(id_of_settimeout)
Parametri
Parameter | Description |
timeout id | Required. The id returned by the setTimeout() method. |
Valore di ritorno
NESSUNO |
Altri esempi
Questo esempio ha un pulsante "Start" per avviare un timer, un campo di immissione per un contatore e un pulsante "Stop" per arrestare il timer:
<button onclick="startCount()">Start count!</button>
<input type="text" id="demo">
<button onclick="stopCount()">Stop count!</button>
<script>
let counter = 0;
let timeout;
let timer_on = 0;
function timedCount() {
document.getElementById("demo").value = counter;
counter++;
timeout = setTimeout(timedCount, 1000);
}
function startCount() {
if (!timer_on) {
timer_on = 1;
timedCount();
}
}
function stopCount() {
clearTimeout(timeout);
timer_on = 0;
}
</script>
Supporto browser
clearTimeout()
è supportato in tutti i browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |