Front end developer specialising in JavaScript and React. Experienced in all aspects of modern front end development. Passionate about making accessible, secure and performant software.
Unfortunately I found it a bit hard to tell what's wrong because I can't see the whole thing to run it and debug it. It seems like it should be working.
Nevertheless, consider trying something like this to keep the code more clear and maybe fix the issues (depending on what the rest of the code is dong):
constreqRef=useRef()constpreviousTimeRef=useRef()constanimate=time=>{// some animationpreviousTimeRef.current=timereqRef.current=requestAnimationFrame(animate)}useEffect(()=>{if(!shouldStop){reqRef.current=requestAnimationFrame(animate)}return()=>cancelAnimationFrame(reqRef.current)},[shouldStop])
Changes made:
Don't have a cancelAnimationFrame call in animate if animate doesn't decide when the animation will finish. It looks like only the value of shouldStop decides whether the animation should run or not, which is not changed inside animate.
In useEffect, don't start a new animation if it shouldn't be running.
Another suggestion, consider changing shouldStop (a negative condition) to isAnimating (a positive condition) and invert the conditionals. Working with double negative conditions like if (!shouldStop) is more difficult than working with positive conditions if (isRunning).
I guess I should clarify that in fact I took the requestAnimationFrame() part from someone's example and didn't really change it as long as it's working. Just provided this condition to link the animation to the Stop button.
The way I understand it, requestAnimationFrame() does not work as a one line function, it has this intricate way of operating. It works only withing this looping function, generates the reference id with each run. And — I discovered it just now after more research — generates the timestamp each time and passes it back as an argument (time in my example).
Unfortunately I found it a bit hard to tell what's wrong because I can't see the whole thing to run it and debug it. It seems like it should be working.
Nevertheless, consider trying something like this to keep the code more clear and maybe fix the issues (depending on what the rest of the code is dong):
Changes made:
cancelAnimationFrame
call inanimate
ifanimate
doesn't decide when the animation will finish. It looks like only the value ofshouldStop
decides whether the animation should run or not, which is not changed insideanimate
.useEffect
, don't start a new animation if it shouldn't be running.Another suggestion, consider changing
shouldStop
(a negative condition) toisAnimating
(a positive condition) and invert the conditionals. Working with double negative conditions likeif (!shouldStop)
is more difficult than working with positive conditionsif (isRunning)
.Good luck :)
Hey Spyros, thanks for suggestions!
I guess I should clarify that in fact I took the requestAnimationFrame() part from someone's example and didn't really change it as long as it's working. Just provided this condition to link the animation to the Stop button.
The way I understand it,
requestAnimationFrame()
does not work as a one line function, it has this intricate way of operating. It works only withing this looping function, generates the reference id with each run. And — I discovered it just now after more research — generates the timestamp each time and passes it back as an argument (time
in my example).You didn't ask 😜, but if you're interested, here's a great reference I found css-tricks.com/using-requestanimat...