本文实例为大家分享了vue+moment实现倒计时的具体代码,供大家参考,具体内容如下
示例
代码
<!-- 使用计算属性,传入截止日期 --> <span>{{countDown(endDate)}}</span>
/*引入日期插件*/ import moment from \'moment\' export default { data() { return { now: moment(), endDate: \'2019-05-07 08:20:00\', } }, mounted() { //定时更新当前时间 setInterval(()=>{ this.now = moment() },1000), //数字前补 0 // num传入的数字,n需要的字符长度 PrefixInteger(num, n) { return (Array(n).join(0) + num).slice(-n); } }, computed: { //计算属性,返回剩余时间 countDown(){ return function(endDate) { let m1 = this.now let m2 = moment(endDate) var du = moment.duration(m2 - m1, \'ms\'), hours = du.get(\'hours\'), mins = du.get(\'minutes\'), ss = du.get(\'seconds\'); if(hours<=0 && mins<=0 && ss<=0) { return \"已超时\" }else { return this.PrefixInteger(hours,2) + \':\' + this.PrefixInteger(mins,2) + \':\' + this.PrefixInteger(ss,2) } } } }, }
更多关于倒计时的文章请查看专题:《倒计时功能》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
© 版权声明
THE END
暂无评论内容