N分钟倒计时

本文介绍了如何使用JavaScript编写一个简单的倒计时函数,通过时间戳计算剩余时间并每秒更新,当时间结束时清空定时器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

let restTime = "" // 剩余时间
let timeStamp = 60000 // 剩余时间戳(默认一分钟)
let timer = null

// 时间戳转时分秒
function toHHmmss(data) {
  let s = ''
  const minutes = parseInt((data % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = (data % (1000 * 60)) / 1000
  s = (minutes < 10 ? ('0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
  return s
}

// 获取剩余时间
function getRestTime() {
  // 赋值(转时分秒)
  restTime = toHHmmss(timeStamp)
  // 大于0秒
  if (timeStamp >= 1000) {
    timeStamp = timeStamp - 1000
  } else {
    // 最后一秒将剩余时间修改
    restTime= "00:00"
    // 清除定时器
    clearInterval(timer)
  }
  console.log('==========(restTime)========>>>', restTime)
}

timer = setInterval(getRestTime, 1000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值