vue time倒计时

安装 npm i moment -s   

<!-- 使用计算属性,传入截止日期 -->
<span>{{countDown(endDate)}}</span>
1
2
/*引入日期插件*/
import moment from 'moment'
export default {
	data() {
	  return {
	    now: moment(),
	    endDate: '2019-05-07 08:20:00',
	  }
	},
	mounted() {
	  //定时更新当前时间
	 setInterval(()=>{
	   this.now = moment()
	 },1000),
	},
	computed: {
	  //计算属性,返回剩余时间
	  countDown(endDate){
	    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 {
	       		完成时
	      }
	    }
	  }
	},
	methods:{
		  //数字前补 0 
	  // num传入的数字,n需要的字符长度
			 PrefixInteger(num, n) {
			   return (Array(n).join(0) + num).slice(-n);
			}
	}
}
————————————————
版权声明:本文为CSDN博主「Aesopcmc」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u014438244/article/details/89915073
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中实现验证码倒计时功能的关键是使用定时器来更新倒计时的时间,并根据时间的变化来控制按钮的状态和显示文本。以下是一个实现验证码倒计时功能的Vue代码示例: ```html <template> <button :disabled="isSend" @click="countDown">{{ codeName }}</button> </template> <script> export default { data() { return { isSend: false, codeName: "发送验证码", totalTime: 10, timer: null }; }, methods: { countDown() { if (this.isSend) return; this.isSend = true; this.codeName = this.totalTime + 's后重新发送'; this.timer = setInterval(() => { this.totalTime--; this.codeName = this.totalTime + 's后重新发送'; if (this.totalTime < 0) { clearInterval(this.timer); this.codeName = '重新发送验证码'; this.totalTime = 10; this.isSend = false; } }, 1000); } } }; </script> ``` 在上述代码中,我们使用了一个`isSend`变量来控制按钮的禁用状态,`codeName`变量来控制按钮的显示文本,`totalTime`变量来表示倒计时的总时间,`timer`变量来保存定时器的引用。 在`countDown`方法中,我们首先判断如果已经发送过验证码,则直接返回。然后将`isSend`设置为`true`,将`codeName`设置为倒计时的初始文本。接着使用`setInterval`函数创建一个定时器,每隔1秒更新一次倒计时的时间和按钮的显示文本。当倒计时结束时,清除定时器,将`codeName`恢复为重新发送的文本,将`totalTime`重置为初始值,将`isSend`设置为`false`。 这样,当用户点击按钮时,就会触发倒计时功能,按钮会进入禁用状态,并显示倒计时的时间。当倒计时结束后,按钮恢复为可点击状态,并显示重新发送的文本。 #### 引用[.reference_title] - *1* [vue实现验证码倒计时](https://blog.csdn.net/qq_41793354/article/details/121910500)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [vue实现验证码倒计时功能](https://blog.csdn.net/weixin_48168510/article/details/119873604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值