js倒计时|天时分秒| countTime

js倒计时|天时分秒| countTime

 

<!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>

 

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();

 

posted @ 2020-05-19 09:08  Tommy_marc  阅读(1031)  评论(0编辑  收藏  举报