有没有办法使用javascript创建一个倒计时计时器,一旦计时到0,它就会自动调整到不同的倒计时?(示例:从当前时间到2012年新年的倒计时,一旦倒计时到0,它将变为3月份的生日倒计时)(这可以用作事件日程的倒计时。你可以有一个时间表和倒计时到时间表的下一部分,一旦它完成倒计时到第一部分)
发布于 2012-08-28 09:45:46
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-28 09:56:30
您可以拥有一个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-10-01 06:12:57
当然,在许多倒计时中,你可以尝试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依赖于服务器端时区,默认值为“欧洲/伦敦”,您可以通过添加(在其他设置中)、时区:“欧洲/奥斯陆”或任何您喜欢的时区来更改它。
https://stackoverflow.com/questions/12151640
复制相似问题