-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-timeouts-and-intervals.js
More file actions
177 lines (141 loc) · 5.52 KB
/
02-timeouts-and-intervals.js
File metadata and controls
177 lines (141 loc) · 5.52 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 02-timeouts-and-intervals.js
// Demonstrates setTimeout, setInterval, and best practices
console.log("⏰ Starting demonstration of timeouts and intervals\n");
// ============================================================================
// SETTIMEOUT EXAMPLES
// ============================================================================
console.log("1️⃣ Basic setTimeout example:");
setTimeout(() => {
console.log("🎯 This runs after 2 seconds");
}, 2000);
console.log("2️⃣ setTimeout with parameters:");
setTimeout((name, age) => {
console.log(`👋 Hello ${name}, you are ${age} years old`);
}, 1000, "Alice", 25);
console.log("3️⃣ Canceling a timeout:");
const timeoutId = setTimeout(() => {
console.log("❌ This won't run - timeout was canceled");
}, 5000);
// Cancel the timeout after 1 second
setTimeout(() => {
clearTimeout(timeoutId);
console.log("⏹️ Timeout canceled");
}, 1000);
// ============================================================================
// SETINTERVAL EXAMPLES
// ============================================================================
console.log("\n4️⃣ Basic setInterval example:");
let counter = 0;
const intervalId = setInterval(() => {
counter++;
console.log(`🔄 Interval count: ${counter}`);
if (counter >= 5) {
clearInterval(intervalId);
console.log("⏹️ Interval stopped after 5 iterations");
}
}, 1000);
// ============================================================================
// IMPORTANT NOTES AND BEST PRACTICES
// ============================================================================
console.log("\n5️⃣ Important notes about timers:");
// ❌ Minimum delay, not guaranteed
setTimeout(() => {
console.log("⚠️ This might take longer than 1 second if call stack is busy");
}, 1000);
// ✅ Recursive setTimeout (preferred over setInterval)
function recursiveTimeout() {
console.log("🔄 Recursive timeout running...");
setTimeout(recursiveTimeout, 1000); // Guarantees 1s between executions
}
// Start recursive timeout
setTimeout(() => {
console.log("🔄 Starting recursive timeout (will run 3 times):");
let count = 0;
function limitedRecursive() {
count++;
console.log(`🔄 Recursive execution ${count}`);
if (count < 3) {
setTimeout(limitedRecursive, 1000);
}
}
limitedRecursive();
}, 6000);
// ============================================================================
// SETTIMEOUT(0) DEMONSTRATION
// ============================================================================
console.log("\n6️⃣ setTimeout(0) demonstration:");
console.log("🔄 Starting synchronous operation...");
// Simulate a long synchronous operation
const start = Date.now();
while (Date.now() - start < 1000) {
// Busy waiting for 1 second
}
setTimeout(() => {
console.log("⏰ setTimeout(0) callback - this runs after sync code");
}, 0);
console.log("✅ Synchronous operation complete");
// ============================================================================
// PERFORMANCE COMPARISON
// ============================================================================
console.log("\n7️⃣ Performance comparison:");
// ❌ setInterval can overlap if execution takes longer than interval
let intervalCount = 0;
const problematicInterval = setInterval(() => {
intervalCount++;
console.log(`❌ Interval ${intervalCount} - might overlap if slow`);
// Simulate slow execution
const start = Date.now();
while (Date.now() - start < 500) {
// Busy waiting for 500ms
}
if (intervalCount >= 3) {
clearInterval(problematicInterval);
console.log("⏹️ Problematic interval stopped");
}
}, 1000);
// ✅ Recursive setTimeout guarantees timing
setTimeout(() => {
console.log("✅ Starting recursive setTimeout (guarantees timing):");
let recursiveCount = 0;
function guaranteedTiming() {
recursiveCount++;
console.log(`✅ Recursive ${recursiveCount} - guaranteed 1s between`);
// Simulate slow execution
const start = Date.now();
while (Date.now() - start < 500) {
// Busy waiting for 500ms
}
if (recursiveCount < 3) {
setTimeout(guaranteedTiming, 1000);
}
}
guaranteedTiming();
}, 5000);
// ============================================================================
// REAL-WORLD EXAMPLE
// ============================================================================
console.log("\n8️⃣ Real-world example - polling for data:");
function pollForData() {
console.log("📡 Polling for new data...");
// Simulate API call
setTimeout(() => {
const hasNewData = Math.random() > 0.7; // 30% chance of new data
if (hasNewData) {
console.log("📊 New data received!");
// Stop polling when we get data
} else {
console.log("⏳ No new data, polling again in 2 seconds...");
setTimeout(pollForData, 2000);
}
}, 1000);
}
// Start polling after 8 seconds
setTimeout(() => {
console.log("🔄 Starting data polling example:");
pollForData();
}, 8000);
console.log("\n📊 Expected behavior:");
console.log("- Timers will execute in order of their delays");
console.log("- Recursive setTimeout provides better timing guarantees");
console.log("- setInterval can overlap if execution is slow");
console.log("- setTimeout(0) defers execution until after current sync code");