You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -512,7 +512,8 @@
512
512
| 474 |[What is module scope in JavaScript?](#what-is-module-scope-in-javascript)|
513
513
| 475 |[What are shadowing and illegal shadowing?](#what-are-shadowing-and-illegal-shadowing)|
514
514
| 476 |[Why is it important to remove event listeners after use?](#why-is-it-important-to-remove-event-listeners-after-use)|
515
-
| 477 |[What is the difference between setTimeout and requestAnimationFrame in JavaScript, and when should you use each?](#what-is-the-difference-between-settimeout-and-requestanimationframe-in-javascript-and-when-should-you-use-each)|
515
+
| 477 |[What is the difference between setTimeout and requestAnimationFrame, and when should you use each?](#what-is-the-difference-between-settimeout-and-requestanimationframe-and-when-should-you-use-each)|
516
+
516
517
<!-- TOC_END -->
517
518
518
519
<!-- QUESTIONS_START -->
@@ -9533,22 +9534,20 @@ Common use cases and benefits:
9533
9534
9534
9535
**[⬆ Back to Top](#table-of-contents)**
9535
9536
9536
-
477. ### What is the difference between setTimeout and requestAnimationFrame in JavaScript, and when should you use each?
9537
-
9538
-
In JavaScript:
9537
+
477. ### What is the difference between setTimeout and requestAnimationFrame, and when should you use each?
9539
9538
9540
-
- `setTimeout` schedules a function to execute after a specified delay (in milliseconds), regardless of the browser’s rendering cycle.
9541
-
- `requestAnimationFrame` schedules a function to run **before the next repaint**, making it ideal for smooth animations and UI updates.
9539
+
- `setTimeout` schedules a function to execute after a specified delay (in milliseconds), independent of the browser’s rendering cycle.
9540
+
- `requestAnimationFrame` schedules a function to run **right before the next repaint**, making it ideal for smooth animations and efficient visual updates.
9542
9541
9543
9542
**When to use each:**
9544
9543
9545
-
1. **setTimeout**:
9546
-
- Use for delayed tasks or operations that do not need to sync with the screen refresh.
9547
-
- Example: Polling a server every few seconds.
9544
+
1. **setTimeout**
9545
+
- Use for delayed tasks or operations that don’t need to sync with the display refresh rate.
9546
+
- Example: Triggering periodic updates, polling data, or debouncing inputs.
9548
9547
9549
-
2. **requestAnimationFrame**:
9550
-
- Use for animations or visual updates to ensure smooth, performant rendering.
9551
-
- Example: Animating a moving object on the screen.
9548
+
2. **requestAnimationFrame**
9549
+
- Use for animations or visual changes tied to frame rendering for smoother performance.
9550
+
- Example: Moving an element across the screen or updating canvas drawings.
0 commit comments