Skip to content

Instantly share code, notes, and snippets.

@kyleshevlin
Last active December 5, 2016 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleshevlin/3e0b1479f88eb647fab10ec67893e68c to your computer and use it in GitHub Desktop.
Save kyleshevlin/3e0b1479f88eb647fab10ec67893e68c to your computer and use it in GitHub Desktop.
A clock written in Svelte
<h2>Svelte Clock</h2>
<p>Look how fast Svelte goes!</p>
<p>{{hours}}:{{leftPad(minutes, 2, '0'}}:{{leftPad(seconds, 2, '0'}}:{{leftPad(milliseconds, 3, '0'}}</p>
<script>
import leftPad from 'left-pad'
export default {
helpers: {
leftPad
},
data () {
return {
time: new Date()
}
},
computed: {
hours: time => time.getHours(),
minutes: time => time.getMinutes(),
seconds: time => time.getSeconds(),
milliseconds: time => time.getMilliseconds()
},
onrender () {
this.interval = setInterval(() => {
this.set({ time: new Date() })
}, 50)
},
onteardown () {
clearInterval(this.interval)
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment