倒计时缓存时间问题总结

业务场景:

在详情页点击按钮之后,按钮禁用,过3s后按钮才能点击。

我用的vue,大列表点进去的详情页。

 1 <el-button" @click="checkBtn" :disabled="checkdisabled">点此检测 </el-button>

 2 checkBtn() { //点击事件
 3   this.countDown(180);
 4   localStorage.setItem(id, Date.now()) //把点击按钮时的时间存到localStorage
 5 },
 6 countDown(time) {
 7   const timeNum = time //倒计时
 8   this.checkdisabled = true //禁用
 9   const timer = setInterval(() => { //倒计时
10     this.timeNum--
11     if (this.timeNum <= 0) { //达到3分钟时停止倒计时
12       clearInterval(timer)
13       this.checkdisabled = false
14       localStorage.removeItem(id)
15     }
16   }, 1000)
17 },
18 mounted() {
19   const preTime = localStorage.getItem(id) //获取点击按钮时的时间
20   if (!preTime) return
21   const timeNum = parseInt((Date.now() - preTime) / 1000) //当前时间跟点击按钮时候的时间差,并算成s
22   if (timeNum >= 180) {
23     如果这个时间差大于180s, 则证明过了3分钟,
24     localStorage.removeItem(id)
25   } else { //否则,将剩余的时间传进去,继续倒计时
26     this.countDown(180 - timeNum)
27   }
28 }
29 
30 需要注意的是, 把时间存localStorage里是为了, 去了别的页面时依然计算倒计时。
最后的落幕: 推荐一下腾讯云的服务器, 搞活动真的很便宜啦, 3 折3折
https: //cloud.tencent.com/redirect.php?redirect=1014&cps_key=0fe886ef07870a50bf9a175e21a71046&from=console
阿里云的对比一下下啦, 虽然2折

https: //promotion.aliyun.com/ntms/act/qwbk.html?userCode=d3wdmq73

 

转载于:https://www.cnblogs.com/woniubushinide/p/10250020.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值