Repository navigation
Expand file tree
/
Copy path12_async_func_1.html
More file actions
84 lines (71 loc) · 2.61 KB
/
Copy path12_async_func_1.html
File metadata and controls
84 lines (71 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!-- frist example on async for countdown timer -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Count down timer </title>
</head>
<body>
<h1 id="display">10</h1>
<button id="startbtn">Start</button>
<button id="stopbtn">Stop</button>
<button id="resetbtn">Reset</button>
<br/>
<br/>
<!-- html for popup notification -->
<button id="showbtn"> Show Notification </button>
<button id="cancelbtn"> canceal Auto-hide</button>
<div id="toast" style="display: none;"> Task Saved Successfully</div>
<script >
let timeout = 10;
let intervalid = null;
const display = document.getElementById("display");
function startTime (){
if (intervalid !== null) return ; // already run then not start again
intervalid = setInterval(()=>{
timeout--;
if(timeout <= 0){
clearInterval (intervalid);//close interval after time ends
intervalid = null;
display.textContent = "Time`s up";
return;
}
display.textContent = timeout;
},1000);
}
function stopTimer(){
clearInterval(intervalid);
intervalid = null;
}
function resetTimer(){
clearInterval (intervalid);
intervalid = null;
timeout = 10;
display.textContent = timeout;
}
document.getElementById("startbtn").addEventListener("click",startTime);
document.getElementById("stopbtn").addEventListener("click",stopTimer);
document.getElementById("resetbtn").addEventListener("click",resetTimer);
// js for second project
const toast = document.getElementById("toast");
let timeoutId = null;
function showTest(){
toast.style.display="block";
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
toast.style.display="none";
timeoutId = null;
}, 3000);
}
function cancelHide(){
clearTimeout(timeoutId);
toast.style.display="none";
timeoutId = null;
console.log("auto-hide cancealled");
}
document.getElementById("showbtn").addEventListener("click",showTest);
document.getElementById("cancelbtn").addEventListener("click",cancelHide);
</script>
</body>
</html>