Skip to content

Commit 04535eb

Browse files
authored
Merge pull request #27 from amrlabib/feature/running-state
feature/running-state
2 parents b739db7 + 409a375 commit 04535eb

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

app/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function MyTimer({ expiryTimestamp }) {
88
minutes,
99
hours,
1010
days,
11+
isRunning,
1112
start,
1213
pause,
1314
resume,
@@ -22,6 +23,7 @@ function MyTimer({ expiryTimestamp }) {
2223
<div style={{ fontSize: '100px' }}>
2324
<span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span>
2425
</div>
26+
<p>{isRunning ? 'Running' : 'Not running'}</p>
2527
<button type="button" onClick={start}>Start</button>
2628
<button type="button" onClick={pause}>Pause</button>
2729
<button type="button" onClick={resume}>Resume</button>

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function MyTimer({ expiryTimestamp }) {
3737
minutes,
3838
hours,
3939
days,
40+
isRunning,
4041
start,
4142
pause,
4243
resume,
@@ -51,6 +52,7 @@ function MyTimer({ expiryTimestamp }) {
5152
<div style={{fontSize: '100px'}}>
5253
<span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span>
5354
</div>
55+
<p>{isRunning ? 'Running' : 'Not running'}</p>
5456
<button onClick={start}>Start</button>
5557
<button onClick={pause}>Pause</button>
5658
<button onClick={resume}>Resume</button>
@@ -90,6 +92,7 @@ export default function App() {
9092
| minutes | number | minutes value |
9193
| hours | number | hours value |
9294
| days | number | days value |
95+
| isRunning | boolean | flag to indicate if timer is running or not |
9396
| pause | function | function to be called to pause timer |
9497
| start | function | function if called after pause the timer will continue based on original expiryTimestamp |
9598
| resume | function | function if called after pause the timer will continue countdown from last paused state |
@@ -112,6 +115,7 @@ function MyStopwatch() {
112115
minutes,
113116
hours,
114117
days,
118+
isRunning,
115119
start,
116120
pause,
117121
reset,
@@ -125,6 +129,7 @@ function MyStopwatch() {
125129
<div style={{fontSize: '100px'}}>
126130
<span>{days}</span>:<span>{hours}</span>:<span>{minutes}</span>:<span>{seconds}</span>
127131
</div>
132+
<p>{isRunning ? 'Running' : 'Not running'}</p>
128133
<button onClick={start}>Start</button>
129134
<button onClick={pause}>Pause</button>
130135
<button onClick={reset}>Reset</button>
@@ -155,6 +160,7 @@ export default function App() {
155160
| minutes | number | minutes value |
156161
| hours | number | hours value |
157162
| days | number | days value |
163+
| isRunning | boolean | flag to indicate if stopwatch is running or not |
158164
| start | function | function to be called to start/resume stopwatch |
159165
| pause | function | function to be called to pause stopwatch |
160166
| reset | function | function to be called to reset stopwatch to 0:0:0:0 |

src/useStopwatch.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ export default function useStopwatch(settings) {
55
const { autoStart } = settings || {};
66

77
const [seconds, setSeconds] = useState(0);
8+
const [isRunning, setIsRunning] = useState(autoStart);
89
const intervalRef = useRef();
910

1011
function clearIntervalRef() {
1112
if (intervalRef.current) {
13+
setIsRunning(false);
1214
clearInterval(intervalRef.current);
1315
intervalRef.current = undefined;
1416
}
1517
}
1618

1719
function start() {
1820
if (!intervalRef.current) {
21+
setIsRunning(true);
1922
intervalRef.current = setInterval(() => setSeconds((prevSeconds) => (prevSeconds + 1)), 1000);
2023
}
2124
}
2225

2326
function pause() {
24-
if (intervalRef.current) {
25-
clearInterval(intervalRef.current);
26-
intervalRef.current = undefined;
27-
}
27+
clearIntervalRef();
2828
}
2929

3030
function reset() {
@@ -44,6 +44,6 @@ export default function useStopwatch(settings) {
4444
}, []);
4545

4646
return {
47-
...Time.getTimeFromSeconds(seconds), start, pause, reset,
47+
...Time.getTimeFromSeconds(seconds), start, pause, reset, isRunning,
4848
};
4949
}

src/useTimer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ export default function useTimer(settings) {
55
const { expiryTimestamp: expiry, onExpire } = settings || {};
66
const [expiryTimestamp, setExpiryTimestamp] = useState(expiry);
77
const [seconds, setSeconds] = useState(Time.getSecondsFromExpiry(expiryTimestamp));
8+
const [isRunning, setIsRunning] = useState(true);
89
const intervalRef = useRef();
910

1011
function clearIntervalRef() {
1112
if (intervalRef.current) {
13+
setIsRunning(false);
1214
clearInterval(intervalRef.current);
1315
intervalRef.current = undefined;
1416
}
@@ -21,6 +23,7 @@ export default function useTimer(settings) {
2123

2224
function start() {
2325
if (!intervalRef.current) {
26+
setIsRunning(true);
2427
intervalRef.current = setInterval(() => {
2528
const secondsValue = Time.getSecondsFromExpiry(expiryTimestamp);
2629
if (secondsValue <= 0) {
@@ -37,6 +40,7 @@ export default function useTimer(settings) {
3740

3841
function resume() {
3942
if (!intervalRef.current) {
43+
setIsRunning(true);
4044
intervalRef.current = setInterval(() => setSeconds((prevSeconds) => {
4145
const secondsValue = prevSeconds - 1;
4246
if (secondsValue <= 0) {
@@ -62,6 +66,6 @@ export default function useTimer(settings) {
6266

6367

6468
return {
65-
...Time.getTimeFromSeconds(seconds), start, pause, resume, restart,
69+
...Time.getTimeFromSeconds(seconds), start, pause, resume, restart, isRunning,
6670
};
6771
}

0 commit comments

Comments
 (0)