Skip to content

Instantly share code, notes, and snippets.

@sassman
Last active February 21, 2023 22:19
Show Gist options
  • Save sassman/4205fba65ee6f98fa7bb953cb7583356 to your computer and use it in GitHub Desktop.
Save sassman/4205fba65ee6f98fa7bb953cb7583356 to your computer and use it in GitHub Desktop.
Timer in svelte
<script>
let s = 0;
let m = 0;
let active = false;
let timer;
function setActive(a) {
active = a;
if(!active) {
clearInterval(timer)
} else {
timer = setInterval(() => {
if(s == 59) {
s = 0;
m += 1;
} else {
s += 1;
}
}, 500);
}
}
</script>
<button on:click={() => setActive(!active)}>
{!active ? 'start' : 'stop'}
</button>
<span>{m.toString().padStart(2,0)}</span>:<span>{s.toString().padStart(2,0)}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment