S
SolidJS•2y ago
TripleSmile

Proper implementation of a timer/stopwatch

I am struggling to understand how to properly implement a timer in a reactive library/framework like SolidJS. What I want is 3 buttons: - start - reset - stop I need to represent time in minutes| seconds | 100ms(1/10 of a second) So, what would be a good way to implement a timer like this? Thank you!
6 Replies
Jasmin
Jasmin•2y ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
TripleSmile
TripleSmileOP•2y ago
yes, exactly like this! Thank you very much! 🙂 I rewrote it to use Date.now() for it to not go out of sync if an application potentially laggs or something like that
Jasmin
Jasmin•2y ago
great idea! makes much more sense
sokey3923
sokey3923•2y ago
@triplesmile should onCleanup not be outside of the effect?
Jasmin
Jasmin•2y ago
in this case we want to clean up the interval if the effect runs again. This ensures that there is always only one interval running or none, if the timer got stopped This is from the onCleanup docs:
Registers a cleanup method that executes on disposal or recalculation of the current reactive scope. Can be used in any Component or Effect.
sokey3923
sokey3923•2y ago
@giyomoon ah thx, thought it has to be called at the first level of a component.

Did you find this page helpful?