Skip to content

Commit d09542d

Browse files
committed
Added a example of setInterval and stopping/Clearing that setInterval.
1 parent d43fe2f commit d09542d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

08_Events/three.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Set Interval</title>
7+
</head>
8+
<body>
9+
<h1></h1>
10+
<button id="start">Start</button>
11+
<button id="stop">Stop</button>
12+
</body>
13+
<script>
14+
// Took a variable globally
15+
let start;
16+
17+
// Made a func. to update h1's inner text
18+
function addText(str) {
19+
document.querySelector('h1').innerHTML += str
20+
}
21+
22+
// Added a addEventListener in which a setInterval to perform a task on every 1 sec
23+
document.querySelector('#start').addEventListener('click', function(){
24+
start = setInterval(addText, 1000, ' Run') // (' Run') parameter passed
25+
})
26+
27+
// Added a addEventListener to stop that interval
28+
document.querySelector('#stop').addEventListener('click', function(){
29+
clearInterval(start)
30+
})
31+
</script>
32+
</html>

0 commit comments

Comments
 (0)