Window clearTimeout ()
ví dụ 1
Cách ngăn myGreating () thực thi:
const myTimeout = setTimeout(myGreating, 3000);
function myGreating() {
document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}
function myStopFunction() {
clearTimeout(myTimeout);
}
Thêm ví dụ bên dưới.
Định nghĩa và Cách sử dụng
Phương clearTimeout()
thức xóa bộ hẹn giờ với
setTimeout()
phương thức.
Ghi chú
Để xóa thời gian chờ, hãy sử dụng id được trả về từ setTimeout ():
myTimeout = setTimeout(function, milliseconds);
Sau đó, bạn có thể dừng quá trình thực thi bằng cách gọi clearTimeout ():
clearTimeout(myTimeout);
Xem thêm:
Cú pháp
clearTimeout(id_of_settimeout)
Thông số
Parameter | Description |
timeout id | Required. The id returned by the setTimeout() method. |
Giá trị trả lại
KHÔNG AI |
Các ví dụ khác
Ví dụ này có nút "Bắt đầu" để bắt đầu bộ hẹn giờ, trường nhập cho bộ đếm và nút "Dừng" để dừng bộ đếm thời gian:
<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>
Hỗ trợ trình duyệt
clearTimeout()
được hỗ trợ trong tất cả các trình duyệt:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |