Nice beginner article @yuridevat and great comment @lukeshiru . I also would rather store the unix timespan than the values individually, it makes easier to do math with it and you wouldn't need even to setTime a new timespan every second, a simple subtraction would do the trick. =D
Building on that, here's a handy hook:
import{useState,useEffect}from"react";constSECOND=1_000;constMINUTE=SECOND*60;constHOUR=MINUTE*60;constDAY=HOUR*24;exportdefaultfunctionuseTimer(deadline,interval=SECOND){const[timespan,setTimespan]=useState(newDate(deadline)-Date.now());useEffect(()=>{constintervalId=setInterval(()=>{setTimespan((_timespan)=>_timespan-interval);},interval);return ()=>{clearInterval(intervalId);};},[interval]);/* If the initial deadline value changes */useEffect(()=>{setTimespan(newDate(deadline)-Date.now());},[deadline]);return{days:Math.floor(timespan/DAY),hours:Math.floor((timespan/HOUR)%24),minutes:Math.floor((timespan/MINUTE)%60),seconds:Math.floor((timespan/SECOND)%60)};}
Yes good point! But as far I remember it deviates very irregularly in the milliseconds, so I tend to ignore it unless it's crucial 😅. But thanks for the video link, I'll watch it to refresh my memory on the subject 😁.
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
Wow! Thank you for your suggestion! Since my tutorial is a beginner tutorial, I won't change it, but will include your comment in the article, because it could be very important for those who want to write the code in a professional way and this comment must not be overlooked in any case!
I myself will study your solution to fully understand it. It's time for me to finally write code in a professional way! You really made my day, thanks again!
With React 18's Automatic Batching this is no longer that important, but I feel it's better to have less different state variables if they are always dependent. Also this approach would make it easier to have really pure getTime function which only returns the object used to set the new state.
To repeat this function, we must clear the interval after each rendering using the clearInterval() function.
This is not entirely true: the returned function in useEffecxt is only called when the component is unmounted (or on changes in the deps, but since there are none in this case...)
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
Shame on me. Thank you very much for this advice, Francois. I should indeed include accessible code in my (future) tutorials from the beginning, and not only for my accessibility articles themselves. I will do my research and implement it. I will keep you posted when I have revised the code.
Thank you for supporting me in getting better at accessibility every day.
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
I've been a professional C, Perl, PHP and Python developer. I'm an ex-sysadmin from the late 20th century. These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
👋 Selftaught Accessibility & Frontend Developer 💻 Dev & UX Accessibility Specialist @Atos | CPACC 🙆 Improving the world with accessible web content ✍️ Content creator, Tips on How To Get Into Tech
Passionate and hardworking Fullstack Developer that loves solving real life problems with computer programs. ...always punching my keyboard until I produce something that amazes me.
Nice beginner article @yuridevat and great comment @lukeshiru . I also would rather store the unix timespan than the values individually, it makes easier to do math with it and you wouldn't need even to
setTime
a new timespan every second, a simple subtraction would do the trick. =DBuilding on that, here's a handy hook:
Then its simple to use:
Yes good point! But as far I remember it deviates very irregularly in the milliseconds, so I tend to ignore it unless it's crucial 😅. But thanks for the video link, I'll watch it to refresh my memory on the subject 😁.
In that case only one
useEffect
is needed:Wow! Thank you for your suggestion! Since my tutorial is a beginner tutorial, I won't change it, but will include your comment in the article, because it could be very important for those who want to write the code in a professional way and this comment must not be overlooked in any case!
I myself will study your solution to fully understand it. It's time for me to finally write code in a professional way! You really made my day, thanks again!
May I suggest a few things?
Date.now()
would do the trick instead ofDate.parse(new Date())
.let interval
could beconst interval
. It's never reassigned.deadline
within the scope of Timer there is no need to use it as a parameter for thegetTime
function.timeDiff
and use an object in the stateWith React 18's Automatic Batching this is no longer that important, but I feel it's better to have less different state variables if they are always dependent.
Also this approach would make it easier to have really pure
getTime
function which only returns the object used to set the new state.This is not entirely true: the returned function in useEffecxt is only called when the component is unmounted (or on changes in the deps, but since there are none in this case...)
Thank you so much for your suggestions, I will insert most of your suggestions (the ones I get 😅) right away!
Doh', I thought that I did not fully understand setInterval/clearInterval and useEffect. I will read about it again and update the information.
Thank you for helping me making this tutorial better, I really appreciate it.
If you have any questions feel free to reach out.
For the HTML, you could put a « timer » role and an attribute tabindex="0" on the container of the timer.
Shame on me. Thank you very much for this advice, Francois. I should indeed include accessible code in my (future) tutorials from the beginning, and not only for my accessibility articles themselves. I will do my research and implement it. I will keep you posted when I have revised the code.
Thank you for supporting me in getting better at accessibility every day.
The explanation is beginner friendly. Kudos
Thanks, Samuel.
Hi, you're missing a closing quote everywhere you mention
<div className="timer>
:)Copy paste 😌. Very attentive, thank you!
Hey Kaio! Could you please remove the link in your comment since this is not the right place to post it, thanks!
Nice peace. Many thanks.