js倒计时的几种方法

第一种,简单时长倒计时

复制代码
data() {
    return {
      timer: null,
      m: 0,
      s: 0,
      time: null,
    }
  },

created() {
    this.resetTime(125); //倒计时125S,根据自己的时长确定
 },


methods: {
    //单纯分钟和秒倒计时
    resetTime(time){
      this.m = Math.floor(time / 60);
      this.m = this.m < 10 ? `0${this.m}` : this.m;
      console.log('this.m--',this.m);
      this.s = Math.floor(time % 60);
      this.timer = setInterval(this.countDown,1000);
    },
    countDown(){
      this.s--;
      this.s < 10 && (this.s = '0' + this.s);
      // this.s = this.s < 10 ? `0${this.s}` : this.s;
      if(this.s.length >= 3){
        this.s = 59;
        this.m = `0${(Number(this.m)-1)}`;
      }
      if(this.m.length >= 3){
        this.m='00';
        this.s='00';
        clearInterval(this.timer);
      }
      this.time = `${this.m}分钟${this.s}秒`;
      console.log(this.m+"分钟"+this.s+"秒");
    },
}
复制代码

第二种,根据后端返回的时间戳,与当前时间进去比较,进行倒计时 

复制代码
data() {
    return {
     timer: null,
      time: null,
    }
  },

created() {
     this.timer = setInterval(this.countTime,1000);
 },

methods: {
    countTime() {
      //获取当前时间
      let date = new Date();
      let now = date.getTime();
      //设置截止时间
      let str="2021/3/15 00:00:00";
      let endDate = new Date(str);
      let end = endDate.getTime();
      //时间差
      let leftTime = end - now;
      //定义变量 d,h,m,s保存倒计时的时间
      let d,h,m,s;
      if (leftTime >= 0) {
        d = Math.floor(leftTime/1000/60/60/24);
        h = Math.floor(leftTime/1000/60/60%24);
        m = Math.floor(leftTime/1000/60%60);
        s = Math.floor(leftTime/1000%60);
      }else {
     clearInterval(this.timer); //为负数时清掉定时器
    }
//递归每秒调用countTime方法,显示动态时间效果
      this.time = `${m}分${s}秒`;
     if(m == 0 && s == 0) {
      clearInterval(this.timer); //时间倒计完清掉计时器,并且跳转,这里根据自己的需求写就好了
      this.$app.$redirect('/register/invallid',true);
     }

    },
}
复制代码

3、最简单的60S倒计时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
data() {
    return {
      countDownTime: 60,
      timeer: null,
    }
},
codeCountDown (){ 
    this.timeer = setTimeout(() => {
        this.countDownTime--;
          if (this.countDownTime < 1) {
            this.countDownTime = 60;
            this.isShow = false;
            clearTimeout(this.timeer);
          } else {
            this.codeCountDown();
          }
      }, 1000);
    },和

第四种,后端接口给了起始时间和失效的时间; 

在发起倒计时的地方直接调用countdownStarts()方法就可以了,timeNum是显示倒时时还有多长时间,放在页面显示就可以;

复制代码
data(){
 return {
  tiemmer: null,
  timeNum: '30 : 00',
  num: 1 * 60,
  }
},

  countdownStarts() {
//当前时间剩余 let beginTime = this.currentTime;//后端给的时间戳,自己页面存一下 let endTime = this.expireTime;//后端给的时间戳,自己页面存一下
    if (beginTime < endTime) { let val = endTime - beginTime; this.num = val/1000;this.countDown(); } else { //时间失效的处理,跳转到失效页面啥的 } },   countDown(){ this.tiemmer = setInterval(() => { this.num--; this.timeNum = this.formatTime2(this.num); if(this.num < 1) { clearInterval(this.tiemmer); //倒计时结束后自己的一些操作处理 } }, 1000); },   formatTime2(time) { // 转换为式分秒 let h = parseInt(time / 60 / 60 % 24) h = h < 10 ? '0' + h : h let m = parseInt(time / 60 % 60) m = m < 10 ? '0' + m : m let s = parseInt(time % 60) s = s < 10 ? '0' + s : s // 作为返回值返回 return `${m} : ${s}`; },
复制代码

 

 

参照: https://www.cnblogs.com/heizai002/p/6862418.html

posted @   维维WW  阅读(6623)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示