Last active
February 21, 2023 22:19
-
-
Save sassman/4205fba65ee6f98fa7bb953cb7583356 to your computer and use it in GitHub Desktop.
Timer in svelte
This file contains 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> | |
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