Svelte · Junior · Question 37 of 76
How do you clean up a timer created in $effect?
$effect(() => {
const id = setInterval(tick, 1000);
return () => clearInterval(id);
});- ✓Correct answer: Return a teardown function; it runs before the effect re-runs and when the component is destroyed
- 2Timers are cleared automatically
- 3Call
onDestroyinside the effect
Explanation
The same applies to subscriptions, observers and event listeners.