2011-05-31 21 views
2

我工作的這個倒計時的結果是:我的倒計時,這樣的數量總是http://apps.facebook.com/tjdcountdown/需要幫助對準一個JavaScript的倒計時

我想補充一個特定的寬度爲每個結果(分鐘,小時,天)完全符合下面的文字。

是否有可能爲每個結果創建一個變量並稍後在我的html代碼中調用它們?

這裏是倒計時腳本:

<script type="text/javascript"> 

var myMonth = "<?= $myMonth ?>"; 
var myDay = "<?= $myDay ?>"; 
var myYear = "<?= $myYear ?>"; 
var myHour = "<?= $myHour ?>"; 
var myMin = "<?= $myMin ?>"; 

dateFuture1 = new Date(2013,03,26,14,15,00); 

function GetCount(ddate,iid){ 

    dateNow = new Date(); //grab current date 
    amount = ddate.getTime() - dateNow.getTime(); //calc milliseconds between dates 
    delete dateNow; 

    // if time is already past 
    if(amount < 0){ 
     document.getElementById(iid).innerHTML="I'm now married!"; 
    } 
    // else date is still good 
    else{ 
     years=0;weeks=0;days=0;hours=0;mins=0;secs=0;out=""; 

     amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs 

     years=Math.floor(amount/31536000);//years (no leapyear support) 
     amount=amount%31536000; 

     weeks=Math.floor(amount/604800);//weeks 
     amount=amount%604800; 

     days=Math.floor(amount/86400);//days 
     amount=amount%86400; 

     hours=Math.floor(amount/3600);//hours 
     amount=amount%3600; 

     mins=Math.floor(amount/60);//minutes 
     amount=amount%60; 

     secs=Math.floor(amount);//seconds 

     out += years +""+((years==1)?"":"")+""; 
     out += weeks +""+((weeks==1)?"":"")+""; 
     if(days != 0){out += days +""+((days==1)?"":"")+"";} 
     if(hours != 0){out += hours +""+((hours==1)?"":"")+"";} 
     out += mins +""+((mins==1)?"":"")+""; 
     out += secs +""+((secs==1)?"":"")+", "; 
     out = out.substr(0,out.length-2); 
     document.getElementById(iid).innerHTML=out; 
     setTimeout(function(){GetCount(ddate,iid)}, 1000); 
    } 
} 

window.onload=function(){ 
    GetCount(dateFuture1, 'countbox1'); 
    //you can add additional countdowns here (just make sure you create dateFuture2 and countbox2 etc for each) 
}; 
</script> 

感謝了很多幫助!

+0

+1 //'殺了毫秒' – pixelbobby 2011-05-31 17:33:41

回答

1

是的,你可以做到這一切。將每個數字放在它自己的div內,並將文本居中在每個div內。爲確保文本居中,不需要編碼。

現在的主要問題是所有數字(年,星期,天,小時等)被擠壓成一個div(iid又名'countbox1'),當他們需要被分離到他們自己的div。

+0

幫助了很多,thanx! – Francis 2011-05-31 19:19:24

0

我會確定數字的長度,然後使用該長度來確定合適的寬度。動態分配內存不好(至少在我看來)。爲了確定長度,我確定有更好的JS方法來做到這一點,但一個簡單的方法是保持除以10,直到值低於1並且計算值被分割的次數。

while(flag){ 
    i++; 
    res = res/10 
    if(res<1) 
    flag=false; 
} 

// i would be your length 
// should make this into a function prob