From cfa3670fa263c84db82f07f5634f30523771e8a9 Mon Sep 17 00:00:00 2001 From: Aayush Kumar Date: Mon, 7 Feb 2022 13:40:23 -0800 Subject: [PATCH] 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 --- beta/src/pages/learn/referencing-values-with-refs.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/beta/src/pages/learn/referencing-values-with-refs.md b/beta/src/pages/learn/referencing-values-with-refs.md index 3084dfc520d..937eac8c445 100644 --- a/beta/src/pages/learn/referencing-values-with-refs.md +++ b/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);