Browse Source

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>
main
Aayush Kumar 3 years ago
committed by GitHub
parent
commit
cfa3670fa2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      beta/src/pages/learn/referencing-values-with-refs.md

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

@ -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);

Loading…
Cancel
Save