<!--时间计时器-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计时器</title>
</head>
<body>
<div id="root">
{{date}}
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var vm = new Vue({
el: "#root",
data: {
date: new Date()
},
mounted: function () {
var _this = this;
this.timer = setInterval(function () {
_this.date = new Date();
}, 1000);// 1000ms时间间隔
console.log(this.timer)
},
beforeDestory: function () {
if (this.timer) {
clearInterval(this.timer)
}
}
})
</script>
</body>
</html>
此处{{date}}输出的时间格式是浏览器默认的时间格式,我用的Google chrome,时间格式如下:
Thu Apr 11 2019 10:08:58 GMT+0800 (中国标准时间)
时间的格式化:waiting......
1. 过滤器格式化