zoukankan      html  css  js  c++  java
  • javascript 倒计时

    // 倒计时
    var fnTimeCountDown = function (time) {
        var time = new Date(time).getTime() - new Date().getTime(), // 倒计时时间转变为毫秒数
            days, hours, min, sec, temp;
    
        if (time <= 0) {
            return null;
        }
    
        days = Math.floor(time / (24*3600*1000));
    
        temp = time % (24*3600*1000); // 不够天的转换成小时
        hours = Math.floor(temp / (3600*1000));
    
        temp = temp % (3600*1000);  // 剩余的分钟数
        min = Math.floor(temp / (60*1000));
    
        temp = temp % (60*1000);
        sec = Math.floor(temp / (1000));
    
        // 格式化:"05"
        var zero = function (n) {
            var n = parseInt(n, 10);
            if (n > 0) {
                if (n <= 9) {
                    n = "0" + n;
                }
                return n;
            } else {
                return "00";
            }
        };
    
        return {
            days: days,
            hours: zero(hours),
            min: zero(min),
            sec: zero(sec)
        };
    };
    
    var future = "2014/01/01 00:00", 
        countobj = null,
        timer = null,
        countdown = function (time, callback) {
            // 倒计时数据对象:countobj
            countobj = fnTimeCountDown(time);
            callback && callback(countobj);
    
            if (countobj) {
                timer = setTimeout(function () {
                    countdown(time, callback);
                }, 1000);
            } else {
                // 倒计时结束
                clearTimeout(timer);
            }
        };
    
    setTimeout(function () {
        countdown(future, function (obj) {
        
            if (obj) {
                // 倒计时应用
                console.log(obj);
            } else {
                // 倒计时结束
                console.log("countdown over!");
            }
        });
    }, 0);
  • 相关阅读:
    uva 11078
    hdu1520(树状dp)
    从Markov Process到Markov Decision Process
    剑指Offer系列编程题详解全集
    L1正则和L2正则的区别详解
    协方差详解
    牛顿法和梯度下降法的比较
    C++ const各种用法总结
    Exploration and Exploitation
    RL Algorithm Components
  • 原文地址:https://www.cnblogs.com/xiankui/p/4087468.html
Copyright © 2011-2022 走看看