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);

    佛为心,道为骨,儒为表,大度看世界; 技在手,能在身,思在脑,从容过生活; 三千年读史,不外功名利禄; 九万里悟道,终归诗酒田园;
  • 相关阅读:
    linux软件名规则
    给php开启mysql扩展
    centos6可用的Apache管理脚本
    Centos下设置redis开机自启动
    拆卸mysql
    如何判断是否在一个网路中
    linux 如何清理僵尸进程
    如何查找僵尸进程并Kill之,杀不掉的要查看父进程并杀之
    一张图告诉你php的命名空间和自动加载
    PHP的文件加载机制到底是什么目录
  • 原文地址:https://www.cnblogs.com/taofx/p/4139406.html
Copyright © 2011-2022 走看看