Skip to content

Commit

Permalink
small fixes to stopwatch codesandbox (reactjs#4110)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aayush-k and gaearon committed Feb 7, 2022
1 parent 4be33b8 commit cfa3670
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions beta/src/pages/learn/referencing-values-with-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit cfa3670

Please sign in to comment.