本文介绍了同一页上有多个倒数计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在使用此脚本()以进行假期倒计时。但是这次我需要在一页上进行2次倒计时。我试过有2个不同的页面,每个页面都有一个脚本,并将它们包含在主页中,但这没用。为此目的有效吗?我尝试编辑一些变量,但无法正常工作。
谢谢。
解决方案
我已经编写了一个简单的倒计时构造函数,可以进一步帮助您。
function countDown( startTime,div,the_event){
var tdiv = document.getElementById(divid)
,start = parseInt(startTime.getTime(),10)
,the_event = the_event || startTime.toLocaleString()
,至;
this.rewriteCounter = function(){
var now = new Date()。getTime()
,diff = Math.round((start-now)/ 1000);
if(startTime> now)
{
tdiv.innerHTML = diff +’秒,直到l’+ the_event;
}
else {clearInterval(to);}
};
this.rewriteCounter();
到= setInterval(this.rewriteCounter,1000);
}
//使用
var count1 = new countDown(new Date('Date('2010/12/11 10:44:59')
,'counter1'
,'明天10:45');
var count2 = new countDown(new Date('2010/12/25')
,counter2'
,'第一个圣诞节');
查看@
I have been using this script ( http://www.dynamicdrive.com/dynamicindex6/dhtmlcount.htm ) for countdown to vacations. But this time i need to have 2 countdowns on ONE page. I tried having 2 different pages with one script in each and include them to the main page, but that did not work.
Anyone know how to edit it or have a script that works for this purpose? I tried editing some of the variables but I were unable to get it to work.
Thanks.
解决方案
I have cooked up a simple countdown Constructor which may help you further.
function countDown(startTime, divid, the_event){
var tdiv = document.getElementById(divid)
,start = parseInt(startTime.getTime(),10)
,the_event = the_event || startTime.toLocaleString()
,to;
this.rewriteCounter = function(){
var now = new Date().getTime()
,diff = Math.round((start - now)/1000);
if (startTime > now)
{
tdiv.innerHTML = diff +' seconds untill ' + the_event;
}
else {clearInterval(to);}
};
this.rewriteCounter();
to = setInterval(this.rewriteCounter,1000);
}
//usage
var count1 = new countDown(new Date('2010/12/11 10:44:59')
,'counter1'
,'tomorrow 10:45');
var count2 = new countDown(new Date('2010/12/25')
,'counter2'
,'first christmas day');
Check it out @jsfiddle
这篇关于同一页上有多个倒数计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!