Phương thức window clearTimeOut()

Phương thức window clearTimeOut()

Định nghĩa và sử dụng
Phương thức clearTimeOut() xóa một cái đếm thời gian được thiết lập với phương thức setTimeOut().
Giá trị ID được trả về bởi phương thức setTimeOut() được sử dụng như là tham số cho phương thức clearTimeOut().

myVar=setTimeOut(“javascript function”, milliseconds);

Sau đó, nếu hàm chưa được thực thi , bạn có thể dừng thực thi bằng cách gọi phương thức clearTimeOut():

clearTimeOut(myVar);

Cú pháp

clearTimeOut(id_of_setTimeOut)

Các giá trị tham số

Tham số                                                               Mô tả
id_of_setTimeOut             Được yêu cầu. Giá trị id của cái đếm thời gian được trả về bởi
phương thức setTimeOut()

Ví dụ:

<!DOCTYPE html>
<html>
<body>

<h1>The Window Object</h1>
<h2>The clearTimeout() Method</h2>

<p>Click the button to prevent the timeout to execute. (You have 3 seconds).</p>

<h2 id=”demo”></h2>

<button onclick=”myStopFunction()”>Stop it</button>

<script>
const myTimeout = setTimeout(myGreeting, 3000);

function myGreeting() {
document.getElementById(“demo”).innerHTML = “Happy Birthday!”
}

function myStopFunction() {
clearTimeout(myTimeout);
}
</script>

</body>
</html>

 

Xem kết quả:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_cleartimeout

Chia sẻ

Leave a Reply

Your email address will not be published. Required fields are marked *