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);
});

Explanation

The same applies to subscriptions, observers and event listeners.

Further reading

Problem with a question

Svelte · Junior · question 37

Test yourself

Take the Svelte · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz