Simple Vue components - local time

in javascript •  6 years ago 

Create a div for Vue to link to and surround the dynamic render variable text in a pair of mustaches

<div id="time">{{ text }}</div>

Create a Date object, and a Vue instance. Link it to the time div, and set the initial value to the local current time.

<script>
var d = new Date();
var clock = new Vue({
el: '#time',
data: {
text: d.toLocaleTimeString()
}
})

Create an interval timing event to recalculate the current time every 1000 milliseconds.
setInterval( function() { d = new Date(); clock.text = d.toLocaleTimeString() },1000);
</script>

Assigning the calculated time to the data property text of the Vue instance clockmeans each time the setInterval event runs, the time element updates the value displayed.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @smk762! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Are you a DrugWars early adopter? Benvenuto in famiglia!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Congratulations @smk762! You received a personal award!

Happy Steem Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Downvote challenge - Add up to 3 funny badges to your board
Vote for @Steemitboard as a witness to get one more award and increased upvotes!