• js倒计时|天时分秒| countTime


    js

    const endTimeObj = new Date("2020-5-1");
    function countTime() {
      //时间差
      var leftTime = endTimeObj - new Date();
      //定义变量 d,h,m,s保存倒计时的时间
      var d, h, m, s;
      if (leftTime < 0) {
        return;
      }
      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);
      const str = `${d}天${h}时${m}分${s}秒`;
      setTimeout(countTime, 1000);
      console.log(str);
      //将倒计时赋值到div中
      //递归每秒调用countTime方法,显示动态时间效果
    }
    countTime();

    html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>倒计时</title>
      </head>
      <body>
        <script type="text/javascript">
          const endTimeObj = new Date("2020-5-1");
          function countTime() {
            //时间差
            var leftTime = endTimeObj - new Date();
            //定义变量 d,h,m,s保存倒计时的时间
            var d, h, m, s;
            if (leftTime < 0) {
              return;
            }
            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);
            const str = `${d}天${h}时${m}分${s}秒`;
            setTimeout(countTime, 1000);
            console.log(str);
            //将倒计时赋值到div中
            //递归每秒调用countTime方法,显示动态时间效果
          }
          countTime();
        </script>
      </body>
    </html>
  • 相关阅读:
    NOIp2018集训test-9-4
    「THUSC 2016」成绩单 & 方块消除 (区间dp)
    NOIp2018集训test-9-2(pm)
    NOIp2018集训test-9-2(am)
    NOIp2018集训test-9-1(pm)
    NOIp2018集训test-9-1(am)
    暑假集训test-8-31(pm)
    暑假集训test-8-31(am)
    暑假集训test-8-30
    day22 笔记
  • 原文地址:https://www.cnblogs.com/yjf713/p/13416917.html
Copyright © 2020-2023  润新知