-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path30 setTimeout function in JS.html
65 lines (53 loc) · 2.42 KB
/
30 setTimeout function in JS.html
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
<!DOCTYPE html>
<html lang="en">
<head><title>setTimeout function in JS</title>
</head>
<body>
<!--
setTimeout():
The setTimeout() method executes a block of code after the specified time.
This method executes the code only once.
The commonly used syntax of JavaScript setTimeout is:
setTimeout(function, milliseconds);
function - a function containing a block of code
milliseconds - the time after which this function is executed
Note: The setTimeout() method is useful when you want to execute a block only
once after some time.
For example, showing a message to a user after the specified time.
-->
<script>
//Example 1: Display a Text Once After 3 Second
function greet(){
console.log("sorry i'm 3 seconds late");
}
setTimeout(greet,3000)// setTimeout calls the greet function after 3 seconds
console.log("this line is written after the below line"); // will be shown first
//The setTimeout() method returns the interval id.
//The returned intervalID is a numeric, non-zero value which identifies the timer created
// by the call to setTimeout() ;
// this value can be passed to clearInterval() to cancel the interval.
// program to display a text using setTimeout method
function greet1() {
console.log('Hello world');
}
let intervalId = setTimeout(greet1, 5000);
console.log('Id: ' + intervalId);
console.log(1);
function callme(){
console.log(2)
setTimeout(()=>{
console.log(3);
},1000) // if it was 0 in place of 1000 the output would be still same
console.log(4)
}
console.log(0);
callme();
console.log(5)
// o/p: 1,0,2,4,5,3
//important:
// ->“Blocking” statements prevent the next statement from running until its execution finishes.
// ->Functions like setTimeout(), setInterval(), promises, network calls, events,
// and all other asynchronous calls are non-blocking statements.
</script>
</body>
</html>