所以我在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>
倒计时在那里并且正在倒计时,但由于某种原因,第二个计数器没有任何日子它只显示为零,它只显示了多少小时,有谁知道为什么我有这个问题?我已经完成了所有事情,我显然错过了一些重要的事情。
感谢您的帮助。
答案 0 :(得分:0)
问题似乎是你在两个函数中操纵相同的(全局)变量,因为它们没有在任何范围内声明。
days=0;hours=0;mins=0;secs=0;out="";
尝试将这些重命名为
days2=0;hours2=0;mins2=0;secs2=0;out2="";
答案 1 :(得分: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>