Skip to content

Commit 4e180fa

Browse files
committed
added timers - setTimeout assignments
1 parent 38b37ad commit 4e180fa

5 files changed

Lines changed: 213 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from "react";
2+
3+
interface NotificationProps {
4+
message: string;
5+
duration?: number;
6+
onDismiss: () => void;
7+
}
8+
9+
const Notification: React.FC<NotificationProps> = ({
10+
message,
11+
duration = 3000, // Default duration: 3000ms
12+
onDismiss,
13+
}) => {
14+
return (
15+
<div className='notification'>
16+
<div className='notification-content'></div>
17+
</div>
18+
);
19+
};
20+
21+
export default Notification;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React, { useState } from "react";
2+
3+
interface SearchInputProps {
4+
onSearch: (query: string) => Promise<void>;
5+
delay?: number;
6+
}
7+
8+
const SearchInput: React.FC<SearchInputProps> = ({
9+
onSearch,
10+
delay = 500, // Default delay of 500ms
11+
}) => {
12+
const [query, setQuery] = useState("");
13+
14+
// TODO: Implement delayed search functionality
15+
// - Handle input changes
16+
// - Set up delay timer
17+
// - Cancel previous timer
18+
// - Trigger search after delay
19+
// - Clean up on unmount
20+
21+
return (
22+
<div className='search-input'>
23+
<input type='text' value={query} onChange={(e) => setQuery(e.target.value)} placeholder='Search...' aria-label='Search input' />
24+
</div>
25+
);
26+
};
27+
28+
export default SearchInput;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from "react";
2+
3+
interface CountdownButtonProps {
4+
onComplete: () => void;
5+
duration?: number;
6+
text: string;
7+
}
8+
9+
const CountdownButton: React.FC<CountdownButtonProps> = ({
10+
onComplete,
11+
duration = 5, // Default 5 seconds countdown
12+
text,
13+
}) => {
14+
// TODO: Implement countdown functionality
15+
// - Set up countdown timer
16+
// - Update countdown display
17+
// - Handle button disable state
18+
// - Execute onComplete callback
19+
// - Clean up on unmount
20+
21+
return (
22+
<button
23+
className='countdown-button'
24+
disabled={false} // TODO: Implement disable logic
25+
onClick={onComplete}>
26+
{text}
27+
</button>
28+
);
29+
};
30+
31+
export default CountdownButton;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from "react";
2+
3+
interface QuizQuestionProps {
4+
question: string;
5+
options: string[];
6+
timeLimit: number;
7+
onTimeout: () => void;
8+
onAnswer: (answer: string) => void;
9+
}
10+
11+
const QuizQuestion: React.FC<QuizQuestionProps> = ({ question, options, timeLimit, onTimeout, onAnswer }) => {
12+
// TODO: Implement quiz functionality
13+
// - Set up countdown timer
14+
// - Handle option selection
15+
// - Show remaining time
16+
// - Handle timeout
17+
// - Clean up timer on unmount
18+
19+
return (
20+
<div className='quiz-question'>
21+
<h3>{question}</h3>
22+
<div className='time-remaining'>Time remaining: {timeLimit} seconds</div>
23+
<div className='options'>
24+
{options.map((option, index) => (
25+
<button key={index} onClick={() => onAnswer(option)} className='option-button'>
26+
{option}
27+
</button>
28+
))}
29+
</div>
30+
</div>
31+
);
32+
};
33+
34+
export default QuizQuestion;

0 commit comments

Comments
 (0)