Skip to content

Commit cfa3670

Browse files
aayush-kgaearon
andauthored
small fixes to stopwatch codesandbox (reactjs#4110)
* small fixes to stopwatch codesandbox noticed that the explanation for the first stopwatch codesandbox mentions "update the time every 10 milliseconds" so updated the codesandbox to reflect that also there's a small nuanced bug in the second stopwatch codesandbox where each call to `handleStart()` sets a new interval without checking if there's already one ongoing. Ie: If the user accidentally double clicks the start button, they set two intervals for updating `now` every 10ms and then intervalRef only retains the second interval ID. Thus, it's impossible to actually stop the timer because `handleStop()` will only clear the latest set interval while the original one will keep executing. * Update referencing-values-with-refs.md * Update referencing-values-with-refs.md * Update referencing-values-with-refs.md Co-authored-by: dan <dan.abramov@gmail.com>
1 parent 4be33b8 commit cfa3670

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

beta/src/pages/learn/referencing-values-with-refs.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ export default function Stopwatch() {
9999
setNow(Date.now());
100100

101101
setInterval(() => {
102-
// Update the current time every 100ms.
102+
// Update the current time every 10ms.
103103
setNow(Date.now());
104-
}, 100);
104+
}, 10);
105105
}
106106

107107
let secondsPassed = 0;
@@ -137,6 +137,8 @@ export default function Stopwatch() {
137137
function handleStart() {
138138
setStartTime(Date.now());
139139
setNow(Date.now());
140+
141+
clearInterval(intervalRef.current);
140142
intervalRef.current = setInterval(() => {
141143
setNow(Date.now());
142144
}, 10);

0 commit comments

Comments
 (0)