实现特定时间与现在时间的差值进行倒计时

    // 参数分别为  开始时间即某个特定时间  时长即多长时间   页面id名将计算好的倒计时放在页面上显 
       示
    time(startTime, timelong, idname) {
      let curTime = new Date(startTime); // curTime: Tue Oct 13 2020 16:44:47 GMT+0800 
      (中国标准时间)
      // 2. 在当前时间curTime变量上加上10分钟
      let startAndtimes = new Date(
        curTime.setMinutes(curTime.getMinutes() + timelong)
      ); //开始时间 +  广播时长  Tue Oct 13 2020 17:54:47 GMT+0800 (中国标准时间)
      let nowTime = new Date();
      // 开始时间加上广播时长 大于  现在时间  开始进行 倒计时
      if (startAndtimes.getTime() > nowTime.getTime()) {
        this.timeInterval(startTime, timelong, idname);
        // console.log(idname, "jjnjjnjnjnj");
      }
    },
    //倒计时方法
    daojishiTime(startTime, timelong, idname) {
      let curTime = new Date(startTime); // curTime: Tue Oct 13 2020 16:44:47 GMT+0800 
      (中国标准时间)
      // 2. 在当前时间curTime变量上加上10分钟
      let startAndtimes = new Date(
        curTime.setMinutes(curTime.getMinutes() + timelong)
      ); //开始时间 +  广播时长  Tue Oct 13 2020 17:54:47 GMT+0800 (中国标准时间)
      let now = new Date();
      // 获取时间差,单位“毫秒”
      let disTime = Math.abs(startAndtimes.getTime() - now.getTime());
      // 判断是否时间到了指定点
      // 是,就终止计时器,终结函数运行。
      // if( disTime===0 ){
      //     showTime.innerHTML = "时间到~!";
      //     clearInterval(myset);
      //     return false;
      // }
      disTime = Math.floor(disTime / 1000); // 把毫秒,转为秒
      let miao = disTime % 60; // 秒
      let fen = Math.floor((disTime / 60) % 60); // 分
      let xiaoshi = Math.floor(disTime / 60 / 60) % 24; // 小时
      let tian = Math.floor(disTime / 60 / 60 / 24); // 天
      // 显示时间。利用了 ES6 的模版字符串,好处是可以存放变量。
      let showTime = document.getElementById(idname);
      showTime.innerHTML = `${tian}天${xiaoshi}时${fen}分${miao}秒`;
      // return `${tian}天${xiaoshi}时${fen}分${miao}秒`;
      // console.log(
      //   `${tian}天${xiaoshi}时${fen}分${miao}秒`,
      //   "-==========================----"
      // );
    },
    // 启动计时器,让时间走动起来。
    timeInterval(startTime, timelong, idname) {
      setInterval(() => {
        this.daojishiTime(startTime, timelong, idname);
      }, 1000);
    },

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值