หน้าต่าง clearTimeout()
ตัวอย่าง 1
วิธีป้องกันไม่ให้ myGreating() ดำเนินการ:
const myTimeout = setTimeout(myGreating, 3000);
function myGreating() {
document.getElementById("demo").innerHTML = "Happy Birthday to You !!"
}
function myStopFunction() {
clearTimeout(myTimeout);
}
ตัวอย่างเพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
วิธี การclearTimeout()
ล้างตัวจับเวลาที่ตั้งไว้ด้วย
setTimeout()
วิธีการ
บันทึก
หากต้องการล้างการหมดเวลา ให้ใช้id ที่ ส่งคืนจาก setTimeout():
myTimeout = setTimeout(function, milliseconds);
จากนั้นคุณสามารถหยุดการดำเนินการได้โดยเรียก clearTimeout():
clearTimeout(myTimeout);
ดูสิ่งนี้ด้วย:
ไวยากรณ์
clearTimeout(id_of_settimeout)
พารามิเตอร์
Parameter | Description |
timeout id | Required. The id returned by the setTimeout() method. |
คืนมูลค่า
ไม่มี |
ตัวอย่างเพิ่มเติม
ตัวอย่างนี้มีปุ่ม "เริ่ม" เพื่อเริ่มตัวจับเวลา ช่องป้อนข้อมูลสำหรับตัวนับ และปุ่ม "หยุด" เพื่อหยุดตัวจับเวลา:
<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>
รองรับเบราว์เซอร์
clearTimeout()
รองรับในทุกเบราว์เซอร์:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |