-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScript_tenth.html
More file actions
50 lines (38 loc) · 1.34 KB
/
JavaScript_tenth.html
File metadata and controls
50 lines (38 loc) · 1.34 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>setInterval and setTimeout in JavaScript</title>
</head>
<body>
<h1 id="time"> Time: </h1>
<h2>setTimeout</h2>
<p>Allows us to run the function only once after the given interval.</p>
<h2>clearTimeout</h2>
<p>Allows us to stop the call of the setTimeout function if not executed.</p>
<h2>setInterval</h2>
<p>Allows us to repeat a function after the given interval.</p>
<h2>clearInterval</h2>
<p>Allows us to stop the call of the setInterval function.</p>
<script>
function greet(name)
{
console.log("Hello", name + "!");
}
let id = setTimeout(greet, 5000, "Saurav"); // time in milliseconds and function name only
// it returns an unique id
clearTimeout(id);
let id1 = setInterval(greet, 2000, "Saurav");
clearTimeout(id1);
function display()
{
let time = document.getElementById('time');
let date = new Date();
time.innerHTML = "Time: " + date;
}
setInterval(display, 1000);
</script>
</body>
</html>