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

    (function (){
        var jtimer = function() {
            // init
            if(arguments.length >= 1) {
                this.setEndTime(arguments[0]);
            }
            if(arguments.length >= 2) {
                this.setGenerateCallBack(arguments[1]);
            }
        };
        jtimer.prototype.setEndTime = function () {
            if(arguments.length == 1) {
                this.endTime = arguments[0]; // Date
            }
        }
        jtimer.prototype.getMillisecond = function () {
            return this.endTime.getTime() - new Date().getTime();
        };
        jtimer.prototype.setGenerateCallBack = function (callback) {
            if(typeof callback == "undefined") return;
            this.generateCallBack = callback;
        }
        jtimer.prototype.generate = function () {
            if(typeof this.generateCallBack == "undefined") return;
            var ms = this.getMillisecond();
            this.generateCallBack(
                Math.floor(ms/(1000 * 60 * 60 * 24)),
                Math.floor(ms/(1000*60*60)) % 24,
                Math.floor(ms/(1000*60)) % 60,
                Math.floor(ms/1000) % 60
            );
        };
        jtimer.prototype.start = function () {
            var delay = 1000;
            if(arguments.length == 1) {
                delay = arguments[0];
            }
            _this = this; // for closure
            this.interval = window.setInterval(
                function() {
                    _this.generate();
                }, delay);
        }
        jtimer.prototype.stop = function () {
            if(typeof this.interval == "undefined") return;
            window.clearInterval(this.interval);
            this.interval = undefined;
        }
    
        window.jtimer = jtimer;
    })();
    
    
    var jt = new jtimer(new Date("6/27/2016"), function (day, hour, min, sec) {
        console.log(day + "," + hour + "," + min + "," + sec);
    });
    jt.start(1000);
  • 相关阅读:
    1.python简介
    JSP标准标签库:JSTL
    冒泡排序算法
    中英文金额大写转换器
    递归与斐波那契数列
    web.xml配置文件详解
    Servlet及相关类和接口
    Servlet初始化及处理HTTP请求
    [转]jqGrid 属性、事件全集
    java web 过滤器跟拦截器的区别和使用
  • 原文地址:https://www.cnblogs.com/TLightSky/p/4063748.html
Copyright © 2011-2022 走看看