Article cover

倒计时练习

需求:计算到2024年生日还有多少时间

  1. 用将来时间减去现在时间就是剩余的时间
  2. 把剩余的时间转换为天、时、分、秒

核心:使用将来的时间戳减去现在的时间戳

注意:

通过时间戳得到是毫秒,需要转换为秒在计算

转换公式:

  • d = parseInt(总秒数/ 60/60 /24); // 计算天数
  • h = parseInt(总秒数/ 60/60 %24) // 计算小时
  • m = parseInt(总秒数 /60 %60 ); // 计算分数
  • s = parseInt(总秒数%60); // 计算当前秒

代码实现

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      background-color: blue;
    }

    .countdown {
      width: 300px;
      height: 310px;
      text-align: center;
      line-height: 1;
      color: #fff;
      background-color: inherit;
      border: 2px solid black;
      border-radius: 15px;
      /* background-size: 240px; */
      /* float: left; */
      overflow: hidden;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
    }

    .countdown .next {
      font-size: 16px;
      margin: 25px 0 14px;
    }

    .countdown .title {
      font-size: 33px;
    }

    .countdown .tips {
      margin-top: 80px;
      font-size: 23px;
    }

    .countdown small {
      font-size: 17px;
    }

    .countdown .clock {
      width: 260px;
      margin: auto;
      overflow: hidden;
    }

    .countdown .clock span,
    .countdown .clock i {
      display: block;
      text-align: center;
      line-height: 34px;
      font-size: 23px;
      float: left;
    }

    .countdown .clock span {
      width: 50px;
      height: 34px;
      border-radius: 2px;
      background-color: #303430;
    }

    .countdown .clock i {
      width: 20px;
      font-style: normal;
    }
  </style>
</head>

<body>
  <div class="countdown">
    <p class="next">今天是年月日</p>
    <p class="title">生日倒计时</p>
    <p class="clock">
      <span id="date">00</span>
      <i>:</i>
      <span id="hour">00</span>
      <i>:</i>
      <span id="minutes">25</span>
      <i>:</i>
      <span id="second">20</span>
    </p>
    <p class="tips">生日时间为2024年8月25日</p>
  </div>
  <script>
    function getRandomColor(flag = true) {
      if (flag) {
        // 3. 如果是true 则返回 #ffffff
        let str = '#'
        let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
        // 利用for循环随机抽6次 累加到 str里面
        for (let i = 1; i <= 6; i++) {
          // 每次要随机从数组里面抽取一个  
          // random 是数组的索引号 是随机的
          let random = Math.floor(Math.random() * arr.length)
          // str = str + arr[random]
          str += arr[random]
        }
        return str

      } else {
        // 4. 否则是 false 则返回 rgb(255,255,255)
        let r = Math.floor(Math.random() * 256)  // 55
        let g = Math.floor(Math.random() * 256)  // 89
        let b = Math.floor(Math.random() * 256)  // 255
        return `rgb(${r},${g},${b})`
      }

    }

    const body = document.querySelector('body')
    body.style.backgroundColor = getRandomColor()

    const next = document.querySelector('.next')
    function getMyDate() {
      const date = new Date()
      let h = date.getHours()
      let m = date.getMinutes()
      let s = date.getSeconds()
      h = h < 10 ? '0' + h : h
      m = m < 10 ? '0' + m : m
      s = s < 10 ? '0' + s : s
      return `今天是${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
    }
    next.innerHTML = getMyDate()
    setInterval(function () {
      div.innerHTML = getMyDate()
    }, 1000)

    function getCountTime() {
      // 当前时间戳
      const now = +new Date()
      // 将来的时间戳
      const last = +new Date('2024-8-25 00:00:00')
      // 获得剩余的时间戳
      const count = (last - now) / 1000
      let d = parseInt(count / 60 / 60 / 24)
      let h = parseInt(count / 60 / 60 % 24) // 计算小时
      let m = parseInt(count / 60 % 60); // 计算分数
      let s = parseInt(count % 60)
      d = d < 10 ? '0' + d : d
      h = h < 10 ? '0' + h : h
      m = m < 10 ? '0' + m : m
      s = s < 10 ? '0' + s : s
      console.log(d, h, m, s);
      document.querySelector('#date').innerHTML = d
      document.querySelector('#hour').innerHTML = h
      document.querySelector('#minutes').innerHTML = m
      document.querySelector('#second').innerHTML = s

    }
    getCountTime()
    setInterval(getCountTime, 1000)
  </script>
</body>

</html>

Comments

Contents

404NotFound

Keyboard shortcuts

Ctrl/⌘+F
Open site search; press again while open for browser Find. Editing areas keep their original behavior.
Shift+D
Toggle appearance, except while editing text.
Esc
Close a theme menu or dismissible overlay.
↑ ↓
Select the previous or next quick action.
← →
Move between the toolbar icons.
Home End
Select the first or last quick action.
Enter Space
Run the selected action.
Tab
Leave the quick menu and continue through the page.

Hold Shift while right-clicking to keep the native menu.

Copy page link