zoukankan      html  css  js  c++  java
  • JavaScript倒计时实现

     1 /**
     2  * 倒计时函数
     3  * @param {String}} endTime 终止时间戳
     4  */
     5 const countDown = (endTime, callback) => {
     6     const end = endTime,
     7         now = new Date().getTime();
     8     let differ = end - now,
     9         hours, minutes, seconds;
    10     if (differ >= 0) {
    11         hours = Math.floor(differ / 1000 / 60 / 60 % 24);
    12         minutes = Math.floor(differ / 1000 / 60 % 60);
    13         seconds = Math.floor(differ / 1000 % 60);
    14     }
    15     callback({
    16         h: check(hours),
    17         m: check(minutes),
    18         s: check(seconds)
    19     });
    20     setTimeout(() => {
    21         countDown(end, callback);
    22     }, 1000);
    23 };
    24 
    25 /**
    26  * 时间前加零
    27  * @param {Number} time
    28  */
    29 const check = time => {
    30     if (time < 10) {
    31         time = '0' + time;
    32     }
    33     return time.toString();
    34 };
    35 
    36 export default countDown;
    1 countDown(new Date('2018/9/5 10:00:00'), function(timeObj) { // 调用
    2       console.log(timeObj.h, timeObj.m, timeObj.s)
    3 });
  • 相关阅读:
    [IOI2013]Dreaming
    Lost Cows
    Mobile Service
    [POI2005]Bank notes
    [CTSC2007]动物园zoo
    [CF1093F]Vasya and Array
    [雅礼集训 2017 Day1]市场
    [APIO2014]序列分割
    [CEOI2004]锯木厂选址
    [APIO2010]特别行动队
  • 原文地址:https://www.cnblogs.com/ljwk/p/9583817.html
Copyright © 2011-2022 走看看