small fixes to stopwatch codesandbox (#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>
This commit is contained in:
Aayush Kumar
2022-02-07 13:40:23 -08:00
committed by GitHub
parent 4be33b8128
commit cfa3670fa2

View File

@@ -99,9 +99,9 @@ export default function Stopwatch() {
setNow(Date.now());
setInterval(() => {
// Update the current time every 100ms.
// Update the current time every 10ms.
setNow(Date.now());
}, 100);
}, 10);
}
let secondsPassed = 0;
@@ -137,6 +137,8 @@ export default function Stopwatch() {
function handleStart() {
setStartTime(Date.now());
setNow(Date.now());
clearInterval(intervalRef.current);
intervalRef.current = setInterval(() => {
setNow(Date.now());
}, 10);