Skip to content

Instantly share code, notes, and snippets.

@jet10000
Forked from sassman/App.svelte
Created September 12, 2021 02:28
Show Gist options
  • Save jet10000/1ddadf4cbc255b700cb66d907c97f1b1 to your computer and use it in GitHub Desktop.
Save jet10000/1ddadf4cbc255b700cb66d907c97f1b1 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