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 π.
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!
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.
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
setTimea 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
useEffectis 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!
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.
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.