2013-05-08 66 views

回答

0

那的JavaScript做你的倒計時:

<script type="text/javascript"> 
//<![CDATA[ 
window.addEvent('domready', function() { 
function calcage(secs, num1, num2, starthtml, endhtml, singular, plural) { 
s = ((Math.floor(secs/num1))%num2).toString(); 
z = ((Math.floor(secs/num1))%num2); 
if (LeadingZero && s.length < 2) 
{ 
s = "0" + s; 
} 
return starthtml 
+ '<div class="sp_countdown_int"> ' 
+ s + '</div>' 
+ '<div class="sp_countdown_string"> ' 
+ ((z<=1)?singular:plural) 
+ '</div>' 
+ endhtml; 
} 
function CountBack(secs) { 
if (secs < 0) { 
document.getElementById("sp_countdown_cntdwn144").innerHTML = '<div class="sp_countdown_finishtext">'+FinishMessage+'</div>'; 
return; 
} 
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000, 
'<div class="sp_countdown_days">','</div>',' Day', ' Days')); 
DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24, 
'<div class="sp_countdown_hours">','</div>',' Hr', ' Hrs')); 
DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60, 
'<div class="sp_countdown_mins">','</div>', ' Min', ' Mins')); 
DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60, 
'<div class="sp_countdown_secs">','</div>', ' Sec', " Secs")); 
document.getElementById("sp_countdown_cntdwn144").innerHTML = DisplayStr; 
if (CountActive) 
setTimeout(function(){ 
CountBack((secs+CountStepper)) 
}, SetTimeOutPeriod); 
} 
function putspan(backcolor, forecolor) { 
} 
if (typeof(BackColor)=="undefined") 
BackColor = ""; 
if (typeof(ForeColor)=="undefined") 
ForeColor= ""; 
if (typeof(TargetDate)=="undefined") 
TargetDate = "07/24/2013 12:00 AM"; 
if (typeof(DisplayFormat)=="undefined") 
DisplayFormat = "%%D%% %%H%% %%M%% %%S%% "; 
if (typeof(CountActive)=="undefined") 
CountActive = true; 
if (typeof(FinishMessage)=="undefined") 
FinishMessage = "Finally we are here"; 
if (typeof(CountStepper)!="number") 
CountStepper = -1; 
if (typeof(LeadingZero)=="undefined") 
LeadingZero = true; 
CountStepper = Math.ceil(CountStepper); 
if (CountStepper == 0) 
CountActive = false; 
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990; 
putspan(BackColor, ForeColor); 
var dthen = new Date(TargetDate); 
var dnow = new Date(); 
if(CountStepper>0) 
ddiff = new Date(dnow-dthen); 
else 
ddiff = new Date(dthen-dnow); 
gsecs = Math.floor(ddiff.valueOf()/1000); 
CountBack(gsecs); 
}); 
//]]> 
</script> 

編輯它來滿足您的需求,這將包括計算秒一個月

DisplayStr = DisplayFormat.replace(/%%MN%%/g, calcage(secs,2592000,12, 
'<div class="sp_countdown_months">','</div>',' Month', ' Months')); 
DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,30, 
'<div class="sp_countdown_days">','</div>',' Day', ' Days')); 

,增加了一個月,並刪除分和秒

DisplayFormat = "%%MN%% %%D%% %%H%%"; 

你看到你的問題是一個月30和31天之間的切換(W/O 2月),使該腳本將需要一個更好的計算,得到這幾個月的權利...

希望這有助於,如果不是我們都需要學習更多關於javaScript;)