Skip to content

Instantly share code, notes, and snippets.

@PuruVJ
Last active August 1, 2021 12:36
Show Gist options
  • Save PuruVJ/f7582a1cc76f1175b46e69e9837fd6a7 to your computer and use it in GitHub Desktop.
Save PuruVJ/f7582a1cc76f1175b46e69e9837fd6a7 to your computer and use it in GitHub Desktop.
Interval as a Svelte Store
<script>
import { createIntervalStore } from './interval.store.ts'
// Create interval of 6 seconds
const interval = createIntervalStore(6000);
$: $interval, doSomething();
</script>
import { readable } from 'svelte/store';
export const createIntervalStore = (duration: number) =>
readable(new Date(), (setTime) => {
let interval = setInterval(() => setTime(new Date()), duration);
return () => clearInterval(interval);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment