-
-
Save jet10000/1ddadf4cbc255b700cb66d907c97f1b1 to your computer and use it in GitHub Desktop.
Timer in svelte
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> | |
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