웹javascript 전단 카운트다운 카운터

4423 단어
작은 카운트다운을 했는데 수준이 비교적 낮고 일부 코드가 최적화되어야 합니다. 읽는 사람의 지적을 환영합니다.
$(function(){
            function countDown(){
                normalelapse = 10;  
                nextelapse = normalelapse;
                counter = 0;   
                startTime = '';
                timer = null;
                time = new Array();
                time[0] = time[1] = time[2] = '00';
            }
            countDown.prototype.run = function(tm){ // 
                var timerFunc = this.onTimer;
                if(tm<10){
                    tm = '0' +tm;
                }
                time[0]     = ''+tm;
                counter     = 0;
                startTime   = new Date().valueOf();
                timer       = setInterval(timerFunc,nextelapse);
            }
            countDown.prototype.insertDom = function(){ //dom  
                var mli  = $('.time-m li');
                var sli  = $('.time-s li');
                var msli = $('.time-ms li');
                mli.empty();
                sli.empty();
                msli.empty();
                armli  = time[0].split('');
                arsli  = time[1].split('');
                armsli = time[2].split('');
                mli.eq('0').append(armli[0]);
                mli.eq('1').append(armli[1]);
                sli.eq('0').append(arsli[0]);
                sli.eq('1').append(arsli[1]);
                msli.eq('0').append(armsli[0]);
                msli.eq('1').append(armsli[1]);
            }
            countDown.prototype.onTimer = function(){ // 
                console.log(time);
                if(time[0]==0&&time[1]==0&&time[2]==0){
	                return;
	            }
	            time[2] -= 1;
	            time[2] += '';  
	            if(time[2]<10&&time[2]>=0){
	                time[2] = '0' + time[2];
	            }
	            if(time[2]<0){
	                time[2] = '99';
	                time[1]-= 1;
	                time[1]+= '';
	                if(time[1]<10&&time[1]>=0){
	                    time[1] = '0' +time[1];
	                }
	                if(time[1]<0){
	                    time[1] = '59';
	                    time[0] -= 1;
	                    time[0] += '';
	                    if(time[0]<10&&time[0]>=0){
	                        time[0] = '0'+time[0];
	                    }
	                }
	            }
                // dom 
                countDown.prototype.insertDom(time);

                clearInterval(timer);
                // 
                counter++;
                var counterSecs = counter * 10;
                var elapseSecs  = new Date().valueOf() - startTime; 
                var diffSecs    = counterSecs  - elapseSecs;  
                nextelapse      = normalelapse + diffSecs;
                if (nextelapse < 0) nextelapse = 0;  

                var timerFunc = countDown.prototype.onTimer;
                timer = setInterval(timerFunc, nextelapse);
                
            }
            var timeDown = new countDown();
            timeDown.run(1);
        });

html 아래
<ul>
                <li>
                    <ul class="time-m">
                        <li class="i-time time-num">0</li>
                        <li class="i-time time-num">0</li>
                    </ul>
                </li>
                <li class="time-d">:</li>
                <li>
                    <ul class="time-s">
                        <li class="i-time time-num">0</li>
                        <li class="i-time time-num">0</li>
                    </ul>
                </li>
                <li class="time-d">:</li>
                <li>
                    <ul class="time-ms">
                        <li class="i-time time-num">0</li>
                        <li class="i-time time-num">0</li>
                    </ul>
                </li>   
            </ul>

좋은 웹페이지 즐겨찾기