-
Notifications
You must be signed in to change notification settings - Fork 5
/
38_setInterval.js
85 lines (79 loc) · 3.3 KB
/
38_setInterval.js
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
85
// Function to explore the intricacies and features of setInterval
function _setInterval() {
/**
* ========================================================
* Basic Syntax and Usage of setInterval
* ========================================================
* The setInterval method schedules repeated execution of code
* at specific intervals. Just like setTimeout, the actual intervals are approximate.
*
* Syntax: setInterval(callback, intervalInMilliseconds, ...additionalArguments)
* - callback: The function that will be executed at each interval.
* - intervalInMilliseconds: The interval time in milliseconds.
* - ...additionalArguments: Optional arguments that are passed to the callback.
*/
const basicIntervalID = setInterval(() => {
console.log("Basic Usage: This message will repeat every 2 seconds.");
}, 2000);
// To stop the above basic example after 6 seconds
setTimeout(() => {
clearInterval(basicIntervalID);
}, 6000);
/**
* ========================================================
* Canceling a Scheduled Interval
* ========================================================
* The setInterval function returns an IntervalID, which can be used to cancel
* the interval using the clearInterval method.
*/
const cancelableIntervalID = setInterval(() => {
console.log("This message will display only once.");
}, 3000);
clearInterval(cancelableIntervalID);
/**
* ========================================================
* Passing Parameters to the Callback
* ========================================================
* setInterval allows passing additional arguments to the callback function.
* These arguments follow the interval parameter in the function signature.
*/
setInterval(
(param1, param2) => {
console.log(`Passed Parameters: ${param1}, ${param2}`);
},
4000,
"Parameter1",
"Parameter2"
);
/**
* ========================================================
* Handling 'this' Context within setInterval
* ========================================================
* The rules for the 'this' context within the callback function are similar
* to those in setTimeout. Arrow functions inherit 'this' from the surrounding
* code, while regular functions don't.
*/
const exampleObject = {
name: "Bob",
greet: function () {
setInterval(() => {
console.log(`Hello, ${this.name}`);
}, 5000);
},
};
exampleObject.greet(); // Output: "Hello, Bob"
/**
* ========================================================
* Nuances and Edge Cases
* ========================================================
* 1. Zero Interval: Even with zero milliseconds as the interval, the actual
* execution may not be immediate due to JavaScript's single-threaded nature.
* 2. Maximum Interval: The maximum length is 2147483647 milliseconds (~24.8 days),
* similar to setTimeout.
*/
setInterval(() => {
console.log("Zero interval doesn't mean immediate repetition.");
}, 0);
}
// Run the exploreSetInterval function to observe the behavior of setInterval
_setInterval();