1

有没有办法使用javascript创建一个倒计时计时器,一旦它达到0,它会自动调整到不同的倒计时?(例如:从当前时间到 2012 年新年的倒计时,一旦达到 0,它将更改为 3 月生日的倒计时)(这可以用作事件时间表的倒计时。你可以有一个时间表和时间表的下一部分倒计时,一旦它完成倒计时到第一部分)

4

3 回答 3

0

当然,在许多倒计时中,您可以尝试jCounter并将其设置为以另一个日期启动插件的另一个实例。

这就是你在 jCounter 中的做法:

$('.counterClass').jCounter({
  date: '31 december 2012 23:59:59', 
  format: 'dd:hh:mm:ss', // display days, hours, minutes and seconds, you can choose here what to display
  fallback: function() { $('.counterClass').jCounter({ // fallback for the next countdown
    date: '31 march 2013 00:00:00', 
    format: 'dd:hh:mm:ss', // format
    fallback : function() { alert("Happy Birthday!") } // second fallback
  }); }
});

请注意,jCounter 依赖于服务器端时区,默认值为“欧洲/伦敦”,您可以通过添加(在其他设置中)、时区:“欧洲/奥斯陆”或任何您喜欢的方式来更改它。

于 2012-09-30T22:12:56.590 回答
0
var timeForNextFunction = false;

function firstFunction(){
    //set timeForNextFunction = true when its time for the next function.
};
function nextFunction(){};

var tick = 100; //milliseconds
document.setInterval(function(){
      if (timeForNextFunction)
          nextFunction();
      else
          firstFucntion();
}, tick);
于 2012-08-28T01:45:45.960 回答
0

你可以有一个数组Date,例如:

function newDate(seconds){
    var d=new Date();
    d.setTime(new Date().getTime()+1000*seconds);
    return d;
}
var events=[newDate(10),newDate(40)];

接着

var currentEvent=0,
    cntdwn=document.getElementById('seconds');
    refreshEvent();
var timer=setInterval(display,1000);

function display(){
    var seconds=Math.ceil((events[currentEvent].getTime()-new Date().getTime())/1000);
    if(seconds>=0){
        cntdwn.innerHTML=seconds;
    }else{
        if(currentEvent<events.length-1){
            currentEvent++;
            refreshEvent();
        }else{
            clearTimeout(timer);
            // Finished
        }
    }
}
function refreshEvent(){
    document.getElementById('currentDate').innerHTML=events[currentEvent];
}

在这里看到它:http: //jsfiddle.net/epUG4/

于 2012-08-28T01:56:29.917 回答