JS案例——下班倒计时

要求:

使用时间戳和定时器完成一个倒计时案例

效果

在这里插入图片描述

分析

设置一个定时器,每隔一秒刷新一次
定时器函数中显示剩余秒数转化为的时间
1. 设置两个时间戳,一个是当前的,另一个是目标时间的
2. 求出距离下班还有多少秒
3. 获取时,分,秒。并将剩余秒数转化为的时间添加到页面中
4. 修改细节

  1. 时间到了关定时器
  2. 修改当前时间

代码

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

    .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"></p>
    <p class="title">下班倒计时</p>
    <p class="clock">
      <span id="hour"></span>
      <i>:</i>
      <span id="minutes"></span>
      <i>:</i>
      <span id="second"></span>
    </p>
    <p class="tips"></p>
  </div>
  <script>
   let hourObj = document.querySelector('#hour')
   let minuteObj = document.querySelector('#minutes')
   let secondObj = document.querySelector('#second')
   let next = document.querySelector('.next')
   let tips = document.querySelector('.tips')
   let flag
   function fn() {
     // 当前时间戳
    let d1 = + new Date()
    // 毕业时间戳
    let d2 = + new Date('2022/4/16 00:00:00')
    let t = (d2 - d1) / 1000 // 转化为秒
    // 倒计时
    // 时分秒公式
    let h = parseInt(t / 60 / 60 % 24) + ''
    let m = parseInt(t / 60 % 60) + ''
    let s = parseInt(t % 60) + ''
    //不足两位补零   并显示在页面中
    hourObj.innerHTML = h.padStart(2 , '0')
    minuteObj.innerHTML = m.padStart(2 , '0')
    secondObj.innerHTML = s.padStart(2 , '0')

    // 显示当前时间
    let date = new Date()
    next.innerHTML = `今天是 ${date.toLocaleDateString()}`
    tips.innerHTML = `当前时间是 ${date.toLocaleTimeString()}`

    // 时间到了关定时器
    if(d2 - d1 <= 0) flag = true
   }

   // 开启定时器
   fn()  //开启定时器之前执行一次避免出现空白
   let timerId = setInterval(fn , 1000)
   if(flag) {
   clearInterval(timerId)
   hourObj.innerHTML = '00'
   minuteObj.innerHTML = '00'
   secondObj.innerHTML = '00'
 }   
  </script>
</body>

</html>
  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

且陶陶º

感谢大人投喂~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值