Last active
August 1, 2021 12:36
-
-
Save PuruVJ/f7582a1cc76f1175b46e69e9837fd6a7 to your computer and use it in GitHub Desktop.
Interval as a Svelte Store
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { createIntervalStore } from './interval.store.ts' | |
// Create interval of 6 seconds | |
const interval = createIntervalStore(6000); | |
$: $interval, doSomething(); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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