vue+moment实现倒计时效果

Dianthe ·
更新时间:2024-09-21
· 779 次阅读

本文实例为大家分享了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) } } } }, }

更多关于倒计时的文章请查看专题:《倒计时功能》



VUE moment 倒计时

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章