Simple Vue components - local time

in #javascript6 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.

Sort:  

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!