|
| 1 | +# JavaScript Timers: Managing Time-Based Operations ⏰ |
| 2 | + |
| 3 | +JavaScript provides several timer functions to execute code after a delay or at regular intervals. These are essential for animations, polling, and managing time-sensitive operations. |
| 4 | + |
| 5 | +## Types of Timers |
| 6 | + |
| 7 | +### 1. setTimeout() - Execute Once After Delay |
| 8 | + |
| 9 | +Executes a function once after a specified delay. |
| 10 | + |
| 11 | +```typescript |
| 12 | +// Basic setTimeout |
| 13 | +setTimeout(() => { |
| 14 | + console.log("Executed after 2 seconds"); |
| 15 | +}, 2000); |
| 16 | + |
| 17 | +// With parameters |
| 18 | +function greet(name: string, greeting: string) { |
| 19 | + console.log(`${greeting}, ${name}!`); |
| 20 | +} |
| 21 | + |
| 22 | +setTimeout(greet, 1000, "John", "Hello"); |
| 23 | +``` |
| 24 | + |
| 25 | +## Practice Assignments: setTimeout in React/Next.js 🎯 |
| 26 | + |
| 27 | +### Assignment 1: Auto-Dismiss Notification |
| 28 | + |
| 29 | +Create a notification component that automatically dismisses after a specified duration. |
| 30 | + |
| 31 | +```typescript |
| 32 | +interface NotificationProps { |
| 33 | + message: string; |
| 34 | + duration?: number; |
| 35 | + onDismiss: () => void; |
| 36 | +} |
| 37 | + |
| 38 | +// TODO: Create a Notification component that: |
| 39 | +// - Shows a message |
| 40 | +// - Automatically dismisses after duration (default 3000ms) |
| 41 | +// - Allows manual dismissal |
| 42 | +// - Cleans up timer on unmount |
| 43 | +``` |
| 44 | + |
| 45 | +### Assignment 2: Delayed Search Input |
| 46 | + |
| 47 | +Implement a search input that delays API calls until user stops typing. |
| 48 | + |
| 49 | +```typescript |
| 50 | +interface SearchInputProps { |
| 51 | + onSearch: (query: string) => Promise<void>; |
| 52 | + delay?: number; |
| 53 | +} |
| 54 | + |
| 55 | +// TODO: Create a SearchInput component that: |
| 56 | +// - Accepts user input |
| 57 | +// - Waits for user to stop typing (default 500ms) |
| 58 | +// - Then triggers search |
| 59 | +// - Cancels previous timeout if user types again |
| 60 | +``` |
| 61 | + |
| 62 | +### Assignment 3: Countdown Button |
| 63 | + |
| 64 | +Implement a button that shows countdown before enabling action. |
| 65 | + |
| 66 | +```typescript |
| 67 | +interface CountdownButtonProps { |
| 68 | + onComplete: () => void; |
| 69 | + duration?: number; |
| 70 | + text: string; |
| 71 | +} |
| 72 | + |
| 73 | +// TODO: Create a CountdownButton that: |
| 74 | +// - Shows countdown in seconds |
| 75 | +// - Disables interaction during countdown |
| 76 | +// - Executes action after countdown |
| 77 | +// - Resets on unmount |
| 78 | +``` |
| 79 | + |
| 80 | +### Assignment 4: Timed Quiz Question |
| 81 | + |
| 82 | +Create a quiz question component with time limit. |
| 83 | + |
| 84 | +```typescript |
| 85 | +interface QuizQuestionProps { |
| 86 | + question: string; |
| 87 | + options: string[]; |
| 88 | + timeLimit: number; |
| 89 | + onTimeout: () => void; |
| 90 | + onAnswer: (answer: string) => void; |
| 91 | +} |
| 92 | + |
| 93 | +// TODO: Create a QuizQuestion component that: |
| 94 | +// - Displays question and options |
| 95 | +// - Shows remaining time |
| 96 | +// - Auto-submits on timeout |
| 97 | +// - Cancels timer if answered early |
| 98 | +// - Cleans up on unmount |
| 99 | +``` |
0 commit comments