Complete the code to create a readable store that starts with the value 0.
import { readable } from 'svelte/store'; const count = readable([1], () => {});
The readable store requires an initial value. Here, 0 is the starting value.
Complete the code to set up a readable store that updates every second with the current time.
import { readable } from 'svelte/store'; const time = readable(new Date(), function start(set) { const interval = setInterval(() => { set([1]); }, 1000); return () => clearInterval(interval); });
The set function needs the current time as a Date object, so new Date() is correct.
Fix the error in the readable store that tries to update a count every second but does not clear the interval.
import { readable } from 'svelte/store'; const count = readable(0, (set) => { const interval = setInterval(() => { set(n => n + 1); }, 1000); return () => [1]; });
To stop a repeating timer created by setInterval, you must call clearInterval with the interval ID.
Fill both blanks to create a readable store that fetches data once and stops when unsubscribed.
import { readable } from 'svelte/store'; const data = readable(null, (set) => { fetch('/api/data') .then(response => response.json()) .then([1] => set([2])); return () => {}; });
The then callback receives the parsed JSON, here named result, which is then passed to set to update the store.
Fill all three blanks to create a readable store that counts seconds and stops after 10 seconds.
import { readable } from 'svelte/store'; const timer = readable(0, (set) => { let count = 0; const interval = setInterval(() => { count += 1; set([1]); if (count === [2]) { [3](interval); } }, 1000); return () => clearInterval(interval); });
The store updates with count, stops after 10 seconds, and clears the interval with clearInterval.