Multiple countdowns on the same page

怎甘沉沦 提交于 2019-12-25 04:45:22

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/4409110/multiple-countdowns-on-the-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!