Jendela clearTimeout()
Contoh 1
Bagaimana mencegah myGreating() untuk mengeksekusi:
const myTimeout = setTimeout(myGreating, 3000);
function myGreating() {
document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}
function myStopFunction() {
clearTimeout(myTimeout);
}
Lebih banyak contoh di bawah ini.
Definisi dan Penggunaan
Metode clearTimeout()
menghapus pengatur waktu yang disetel dengan
setTimeout()
metode.
Catatan
Untuk menghapus batas waktu, gunakan id yang dikembalikan dari setTimeout():
myTimeout = setTimeout(function, milliseconds);
Kemudian Anda dapat menghentikan eksekusi dengan memanggil clearTimeout():
clearTimeout(myTimeout);
Lihat juga:
Sintaksis
clearTimeout(id_of_settimeout)
Parameter
Parameter | Description |
timeout id | Required. The id returned by the setTimeout() method. |
Nilai Kembali
TIDAK ADA |
Lebih Banyak Contoh
Contoh ini memiliki tombol "Mulai" untuk memulai penghitung waktu, bidang input untuk penghitung, dan tombol "Berhenti" untuk menghentikan penghitung waktu:
<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>
Dukungan Peramban
clearTimeout()
didukung di semua browser:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |