写一个下班倒计时的demo

396 阅读1分钟

下班倒计时使用的函数包括:

获取当前日期函数:getMyDate()

获取随机颜色函数:getRandomColor()

定时器函数:setInterval(function (){}, 1000)

获取当前距离下班的时间函数:getCountTime()

实现效果:

未命名-副本.gif

代码如下

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

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    .countdown {
      width: 240px;
      height: 305px;
      text-align: center;
      line-height: 1;
      color: #fff;
      background-color: brown;
      /* background-size: 240px; */
      /* float: left; */
      overflow: hidden;
      margin: 36px 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: 142px;
      margin: 18px auto 0;
      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: 34px;
      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">今天是2022年6月9日</p>
    <p class="title">下班倒计时</p>
    <p class="clock">
      <span id="hour">11</span>
      <i>:</i>
      <span id="minutes">22</span>
      <i>:</i>
      <span id="scond">33</span>
    </p>
    <p class="tips">18:00:00 下班 (๑*◡*๑)</p>
  </div>
  <script>
    // 封装一个今天的日期函数
    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()}${h}:${m}:${s}`
    }
    // 获取元素
    const p = document.querySelector('.countdown .next')
    // 先调用一次的原因是防止页面刷新的时候, 回归到初始内容
    p.innerHTML = getMyDate()
    // 开启定时器 每隔一秒修改一次内容 实现当前时间的自动变化
    setInterval(function () {
      p.innerHTML = getMyDate()
    }, 1000)
    // 每隔一秒换一个背景色
    setInterval(function () {
      // 封装一个随机颜色函数
      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 countdown = document.querySelector('.countdown')
      countdown.style.backgroundColor = getRandomColor()
    }, 1000)


    // 函数封装 getCountTime 得到距离下班还有多少时间
    let timer

    function getCountTime() {
      // 1. 得到当前的时间戳
      const now = +new Date()
      // 2. 得到将来的时间戳 这里的年月日你可以设置比当前的日期大的多的都没事,
      // 因为超出的天数已经自动略去了,只保留时分秒,所以设置大点省的后面去更新。
      const last = +new Date('2022-4-20 18:00:00')
      // 3. 得到剩余的时间戳 count 记得转换为 秒数
      const count = (last - now) / 1000
      // 4. 转换为时分秒
      // d = parseInt(总秒数/ 60/60 /24); // 计算天数
      // h = parseInt(总秒数 / 60 / 60 % 24) // 计算小时
      // m = parseInt(总秒数 / 60 % 60); // 计算分数
      // s = parseInt(总秒数 % 60); // 计算当前秒数
      // let d = parseInt(count / 60 / 60 / 24); // 计算天数

      let h = parseInt(count / 60 / 60 % 24) // 计算小时
      h = h < 10 ? '0' + h : h
      let m = parseInt(count / 60 % 60); // 计算分数
      m = m < 10 ? '0' + m : m
      let s = parseInt(count % 60); // 计算当前秒数
      s = s < 10 ? '0' + s : s
      console.log(h, m, s);

      // 5. 把时分秒写到对应的盒子里面
      const hour = document.querySelector('#hour')
      const minutes = document.querySelector('#minutes')
      const scond = document.querySelector('#scond')
      hour.innerHTML = h
      minutes.innerHTML = m
      scond.innerHTML = s
      // 判断 当时间到的时候,停止计时器, 并把内容清零
      if (count <= 0) {
        clearInterval(timer)
        hour.innerHTML = '00'
        minutes.innerHTML = '00'
        scond.innerHTML = '00'
      }
    }
    // 先调用一次的原因是防止页面刷新的时候, 回归到初始时间
    getCountTime()
    // 开启定时器 每隔一秒获取一次时分秒的时间
    setInterval(getCountTime, 1000)
  </script>
</body>

</html>