Skip to content

Commit 98b886d

Browse files
timeout and clear
Signed-off-by: Arnav Gupta <[email protected]>
1 parent c2e2598 commit 98b886d

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Lecture10/async/delayclick.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
</head>
99
<body>
1010
<button onclick="clickfun()">CLICK ME!</button>
11+
<button disabled id="cancel" onclick="cancel()">CANCEL</button>
1112
<button onclick="otherfun()">OTHER!</button>
1213
<div id="res"></div>
1314
<div id="count"></div>
1415
<script>
1516
let count = 0
17+
let taskId = null;
1618
function clickfun() {
1719
// let start = new Date().getTime()
1820

@@ -21,10 +23,20 @@
2123
// }
2224
// document.getElementById('res').innerText = 'HELLO'
2325

24-
setTimeout(() => {
26+
taskId = setTimeout(() => {
2527
document.getElementById('res').innerText = 'HELLO'
28+
document.getElementById('cancel').setAttribute('disabled', true)
2629
}, 5000)
2730

31+
document.getElementById('cancel').removeAttribute('disabled')
32+
33+
}
34+
35+
function cancel() {
36+
if (taskId) {
37+
clearTimeout(taskId)
38+
document.getElementById('cancel').setAttribute('disabled', true)
39+
}
2840
}
2941

3042
function otherfun() {

0 commit comments

Comments
 (0)