S
SolidJS•17mo 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•17mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
TripleSmile
TripleSmileOP•17mo 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•17mo ago
great idea! makes much more sense
sokey3923
sokey3923•17mo ago
@triplesmile should onCleanup not be outside of the effect?
Jasmin
Jasmin•17mo 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•16mo ago
@giyomoon ah thx, thought it has to be called at the first level of a component.
Want results from more Discord servers?
Add your server