JS小时倒计时

let t1 = new Date("2019-11-26 15:51:00");// 从什么时间开始
let t2 = new Date(t1.setHours(t1.getHours() + 1));// 延迟几个小时
let interval = window.setInterval(() => {
  let t3 = t2 - new Date();// 延迟时间距离当前时间的时间戳
  if (t3 <= 0) {
    alert("时间到!");
    window.clearInterval(interval);
    return;
  }
  let leave1 = t3 % (24 * 3600 * 1000);// 获取毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000));// 获取小时数
  let leave2 = leave1 % (3600 * 1000);// 排除小时的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000));// 获取分钟
  let leave3 = leave2 % (60 * 1000);// 排除分钟的毫秒数
  let seconds = Math.round(leave3 / 1000);// 获取秒
  window.console.log((hours > 9 ? hours : "0" + hours) + ":" + (minutes > 9 ? minutes : "0" + minutes) + ":" + (seconds > 9 ? seconds : "0" + seconds));
}, 500);

 

posted @ 2019-11-26 16:58  _Iniesta  阅读(1596)  评论(0编辑  收藏  举报