캡슐화된 카운트다운 플러그인

19704 단어
HTML
 
DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>title>
    head>
    <body>
         <div id="content">div>
    body>
html>
<script type="text/javascript" src="countDown.js">script>
<script>
    _timeCountDown.openCountDown("content",{//ID      
        startTime:'2016-07-30 10:07:55',//    
        endTime:'2016-07-30 10:08:00',//    ,              
        leftTitle:"   :",//    
        splitDate:false,//      
        colonStat:false,//
        timeCallBack:"lendSellOver()"    //              
    });
    function lendSellOver(){
        alert(11)
    }
script>

 
카운트다운 JS 플러그인
/**
 * @param startTime     
 * @param endTime     
 * @param timeCallBack     
 * 
 *     var _TimeCountDown = new TimeCountDown("sttime1",{    
        startTime:'2016-07-30 10:07:55',        //    
        endTime:'2016-07-30 10:08:00',            //            
        timeCallBack:"timeOverfun",  //    
    });
 */


(function(window,undefined){
    var timeCountDown=function(){};
    timeCountDown.prototype = {
            openCountDown:function(container, params){
                this.opt=params;
                this.container=container;
                this.nTime=this.calculateTime();
                if(!params.splitDate){            
                    this.countdown();
                }else{        
                    this.splitCountdown();            
                }
            },
            countdown:function(){
                    var interval = 1000; //          
                    var time;
                     this.nTime = this.nTime - interval; 
                      var leave1 = this.nTime;//           
                     var t = Math.floor(Math.floor((leave1/(3600*1000))/24));
                      var hleave = this.nTime%((24*3600*1000));
                      //           
                    var h = Math.floor(hleave/(3600*1000)) < 10 ? "0"+ Math.floor(hleave/(3600*1000)) : Math.floor(hleave/(3600*1000));    
                    var leave2 = this.nTime%(3600*1000);        //            
                    //
                    var m = Math.floor(leave2/(60*1000)) < 10 ? "0" + Math.floor(leave2/(60*1000)) : Math.floor(leave2/(60*1000));
                    
                    //
                    var leave3 = leave2%(60*1000);      //            
                    var s = Math.round(leave3/1000) < 10 ? "0" + Math.round(leave3/1000) : Math.round(leave3/1000);    
                     
                     if(!this.opt.colonStat){
                         if(t == 0){
                             time = this.opt.leftTitle+"" + h + "" + m + "" + s + "";
                             
                         }else{
                             
                             time = this.opt.leftTitle+"" + t + "" + h + "" + m + "" + s + "";
                         }                         
                     }else{
                             time = this.opt.leftTitle+"" + h + "" + m + "" + s + "";                     
                     }                
                    var callBackTime = t+h+m+s;
                    document.querySelector("#"+this.container).innerHTML = time;
                    if(callBackTime <= 0){
                        eval(this.opt.timeCallBack);
                        return false;
                    }
                    var that = this;
                    setTimeout(function(){
                        that.countdown();
                    },interval);                
            },
            splitCountdown:function(){
                    var interval = 1000; //          
                    var time;
                     this.nTime = this.nTime - interval; 
                    console.log(this.nTime);
                      var leave1 = this.nTime;//           
                     var t = Math.floor((leave1/(3600*1000))/24) < 10 ? "0" + Math.floor((leave1/(3600*1000))/24) : Math.floor((leave1/(3600*1000))/24);
                      var hleave = this.nTime%((24*3600*1000));
                      //           
                    var h = Math.floor(hleave/(3600*1000)) < 10 ? "0"+ Math.floor(hleave/(3600*1000)) : Math.floor(hleave/(3600*1000));    
                    var leave2 = this.nTime%(3600*1000);        //            
                    //
                    var m = Math.floor(leave2/(60*1000)) < 10 ? "0" + Math.floor(leave2/(60*1000)) : Math.floor(leave2/(60*1000));
                    
                    //
                    var leave3 = leave2%(60*1000);      //            
                    var s = Math.round(leave3/1000) < 10 ? "0" + Math.round(leave3/1000) : Math.round(leave3/1000);    
                    //     
                     var arrT = this.subStr(t+"");
                     //     
                     var arrH = this.subStr(h+"");
                     //     
                     var arrM = this.subStr(m+"");
                     //    
                    var arrS = this.subStr(s+"");
                    if(t == 0){
                        time = this.opt.leftTitle+"" + arrH[0] + "" + arrH[1] + "" + arrM[0] + "" + arrM[1] + "" + arrS[0] + "" + arrS[1] + "";
                    }else{
                        time = this.opt.leftTitle+"" + arrT[0] + "" + arrT[1] + "" + arrH[0] + "" + arrH[1] + "" + arrM[0] + "" + arrM[1] + "" + arrS[0] + "" + arrS[1] + "";
                    }
                    var callBackTime = t+h+m+s;
                    document.querySelector("#"+this.container).innerHTML = time;
                    if(callBackTime <= 0){
                        eval(this.opt.timeCallBack);
                        return false;
                    }
                    var that = this;
                    setTimeout(function(){
                        that.splitCountdown();
                    },interval);                
            }, 
            subStr:function(str){
                var arr = new Array();
                for(var i = 0; i){                
                    arr[i] = str.substring(i,i+1);
                }
                return arr;
            },    
            calculateTime :function(){
                var startTime = this.vert(this.opt.startTime);                
                var endTime = this.vert(this.opt.endTime);
                var time = endTime - startTime;  //        ;
                return time;
            },
            vert:function(time){
                if(typeof time == "undefined" || time==""){
                    return false;
                }
                var strtime = (time).replace(/-/g,"/");
                var date1 = new Date(strtime);
                return parseInt(date1.getTime());            
            }
    }
    window.timeCountDown=timeCountDown;
})(window,undefined);
var _timeCountDown= new timeCountDown();

 
전재 대상:https://www.cnblogs.com/binmengxue/p/6378051.html

좋은 웹페이지 즐겨찾기