0

所以我在 counter.js 中有这段代码

month= --month;
dateFuture = new Date(year,month,day,hour,min,sec);
dateFuture2 = new Date(year2,month2,day2,hour2,min2,sec2);

function GetCount(){

    dateNow = new Date();                                                            
    amount = dateFuture.getTime() - dateNow.getTime()+5;               
    delete dateNow;

    /* time is already past */
    if(amount < 0){
            out=
            "<div id='days'><span></span><p>0</p><div id='days_text'></div></div>" + 
            "<div id='hours'><span></span><p>0</p><div id='hours_text'></div></div>" + 
            "<div id='mins'><span></span><p>0</p><div id='mins_text'></div></div>" + 
            "<div id='secs'><span></span><p>0</p><div id='secs_text'></div></div>" ;
            document.getElementById('countbox').innerHTML=out;       
    }
    /* date is still good */
    else{
            days=0;hours=0;mins=0;secs=0;out="";

            amount = Math.floor(amount/1000); /* kill the milliseconds */

            days=Math.floor(amount/86400); /* days */
            amount=amount%86400;

            hours=Math.floor(amount/3600); /* hours */
            amount=amount%3600;

            mins=Math.floor(amount/60); /* minutes */
            amount=amount%60;


            secs=Math.floor(amount); /* seconds */


            out=
            "<div id='days'><span></span><p>" + days +"</p><div id='days_text'></div></div>" + 
            "<div id='hours'><span></span><p>" + hours +"</p><div id='hours_text'></div></div>" + 
            "<div id='mins'><span></span><p>" + mins +"</p><div id='mins_text'></div></div>" + 
            "<div id='secs'><span></span><p>" + secs +"</p><div id='secs_text'></div></div>" ;
            document.getElementById('countbox').innerHTML=out;


            setTimeout("GetCount()", 1000);
    }
}

month= --month;
dateFuture = new Date(year,month,day,hour,min,sec);
dateFuture2 = new Date(year2,month2,day2,hour2,min2,sec2);

function GetCount2(){

    dateNow = new Date();                                                            
    amount = dateFuture2.getTime() - dateNow.getTime()+5;               
    delete dateNow;

    /* time is already past */
    if(amount < 0){
            out=
            "<div id='days2'><span></span><p>0</p><div id='days_text'></div></div>" + 
            "<div id='hours2'><span></span><p>0</p><div id='hours_text'></div></div>" + 
            "<div id='mins2'><span></span><p>0</p><div id='mins_text'></div></div>" + 
            "<div id='secs2'><span></span><p>0</p><div id='secs_text'></div></div>" ;
            document.getElementById('countbox2').innerHTML=out;       
    }
    /* date is still good */
    else{
            days=0;hours=0;mins=0;secs=0;out="";

            amount = Math.floor(amount/1000); /* kill the milliseconds */

                       hours=Math.floor(amount/499600); /* hours */
            amount=amount%3600;

            mins=Math.floor(amount/60); /* minutes */
            amount=amount%60;


            secs=Math.floor(amount); /* seconds */


            out=
            "<div id='days2'><span></span><p>" + days +"</p><div id='days_text'></div></div>" + 
            "<div id='hours2'><span></span><p>" + hours +"</p><div id='hours_text'></div></div>" + 
            "<div id='mins2'><span></span><p>" + mins +"</p><div id='mins_text'></div></div>" + 
            "<div id='secs2'><span></span><p>" + secs +"</p><div id='secs_text'></div></div>" ;
            document.getElementById('countbox2').innerHTML=out;


            setTimeout("GetCount2()", 1000);
    }
}

window.onload=function(){GetCount(); GetCount2();}

我在 index.html 中有这个代码来给出具体时间的日期,在这种情况下两者都是相同的

     <!-- Enter countdown here -->
<script type="text/javascript">
//Edit data below to your personal preferences ----------------------------------

//Give the date ---------------------------------
year = 2015; year2 = 2015;
month = 12; month2 = 12;
day = 12; day2 = 12;
//Give the point of time ------------------------
hour= 24; hour2 = 24;
min= 0; min2 = 0;
sec= 0; sec2 = 0;
//-----------------------------------------------

//That's it!

这会触发 index.html 中的两个倒计时

<!-- Countdown -->
            <div id="countdown">
                <div class="event-title">
                </div>
                <div id="countbox"></div>
                <div class="event-title"></div>
            </div>


        <div id="countdown2">
            <div class="event-title">
            </div>
            <div id="countbox2"></div>
            <div class="event-title"></div>
        </div>

倒计时在那里并且正在倒计时,但由于某种原因,第二个计数器没有任何天,它只显示为零,它只显示多少小时,有人知道我为什么会遇到这个问题吗?我经历了一切,我显然错过了一些重要的事情。

感谢您的任何帮助。

4

2 回答 2

0

问题似乎是您在两个函数中操作相同的(全局)变量,因为它们没有在任何范围内声明。

days=0;hours=0;mins=0;secs=0;out="";

尝试将这些重命名为

days2=0;hours2=0;mins2=0;secs2=0;out2="";
于 2013-10-22T10:49:55.390 回答
0

简短的回答是您缺少几行代码。特别是在month= --month;您需要month2= --month2;并且在GetCount2功能中您错过了这两行之后:

days=Math.floor(amount/86400); /* days */
amount=amount%86400;

长答案(以及我会推荐的)是使GetCount函数更通用一点,并且只使用一个函数。所以 counter.js 可能看起来像这样:

function GetCount(countdownDate, elementId){
    var dateNow = new Date();
    var amount = countdownDate.getTime() - dateNow.getTime() + 5;               
    delete dateNow;
    var out = "";
    /* time is already past */
    if(amount < 0){
            out =
            "<div id='days'><span></span><p>0</p><div id='days_text'></div></div>" + 
            "<div id='hours'><span></span><p>0</p><div id='hours_text'></div></div>" + 
            "<div id='mins'><span></span><p>0</p><div id='mins_text'></div></div>" + 
            "<div id='secs'><span></span><p>0</p><div id='secs_text'></div></div>" ;
            document.getElementById(elementId).innerHTML=out;       
    }
    /* date is still good */
    else{
            var days=0, hours=0, mins=0, secs=0;

            amount = Math.floor(amount/1000); /* kill the milliseconds */

            days=Math.floor(amount/86400); /* days */
            amount=amount%86400;

            hours=Math.floor(amount/3600); /* hours */
            amount=amount%3600;

            mins=Math.floor(amount/60); /* minutes */
            amount=amount%60;

            secs=Math.floor(amount); /* seconds */

            out=
            "<div id='days'><span></span><p>" + days +" days</p><div id='days_text'></div></div>" + 
            "<div id='hours'><span></span><p>" + hours +" hours</p><div id='hours_text'></div></div>" + 
            "<div id='mins'><span></span><p>" + mins +" mins</p><div id='mins_text'></div></div>" + 
            "<div id='secs'><span></span><p>" + secs +" secs</p><div id='secs_text'></div></div>" ;
            document.getElementById(elementId).innerHTML=out;

            setTimeout(function() { GetCount(countdownDate, elementId); }, 1000);
    }
}

你可以在页面上你需要类似的东西

<script type="text/javascript">
    //Edit data below to your personal preferences ----------------------------------

    //Give the date ---------------------------------
    var year = 2015, year2 = 2015,
    month = 12, month2 = 12;
    day = 12, day2 = 12,
    //Give the point of time ------------------------
    hour= 24, hour2 = 24,
    min= 0, min2 = 0,
    sec= 0, sec2 = 0;
    //-----------------------------------------------

    //That's it!

    month= --month;
    month2= --month2;
    dateFuture = new Date(year,month,day,hour,min,sec);
    dateFuture2 = new Date(year2,month2,day2,hour2,min2,sec2);

    window.onload=function(){
        GetCount(dateFuture, "countdown"); GetCount(dateFuture2, "countdown2");
    }
</script>
于 2013-10-22T11:00:26.400 回答