2012-02-27 24 views
0

基本上我在JavaScript的好,我想爲我的項目更好的倒數計時器。此刻我的定時器看起來是這樣的:JavaScript的PHP的好倒計時

1時25分05秒

我想它看起來就像這樣:

1小時25分鐘,5秒

但我也希望它是聰明的。所以,如果它只需25分鐘,它會說:

25分鐘。


這是我目前的javascript代碼:

function secondCountdown(){ 
    if(typeof(skip) == "undefined"){ 
     skip = "set"; 
    } else { 
     var timeleft = document.getElementById("timeleft").innerHTML; 
     var time_split = timeleft.split(":"); 

     if(parseInt(time_split[0]) < 10){ 
      time_split[0] = time_split[0].charAt(1); 
     } 

     if(parseInt(time_split[1]) < 10){ 
      time_split[1] = time_split[1].charAt(1); 
     } 

     if(parseInt(time_split[2]) < 10){ 
      time_split[2] = time_split[2].charAt(1); 
     } 

     seconds = parseInt(time_split[2]) + parseInt(time_split[1]*60) + parseInt(time_split[0]*3600); 
     seconds -= 1; 

     if(seconds > 0){ 
       var hours= Math.floor(seconds/3600); 
       seconds %= 3600; 
       var minutes = Math.floor(seconds/60); 
       seconds %= 60; 

       var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds; 

      document.getElementById("timeleft").innerHTML = timeleft; 
     } else { 
      document.getElementById("timeleft").innerHTML = ""; 
      window.location='test.php?pageid=' + getURLParam("pageid"); 
      return false; 
     } 
    } 

    setTimeout("secondCountdown()",1000); 
} 
window.onload = secondCountdown; 

也許有人可以編輯對我來說,使它輸出我想要什麼?

編輯:

這是計時器的PHP端。

<span id="timeleft"> 
$master = $timer['gta_time'] - time(); 

       echo date("00:i:s", $timer['gta_time'] - time()); 


</span> 
+1

你在PHP或JavaScript想這個? – Brad 2012-02-27 23:05:18

+0

嗯,我想腳本是在JS中,但時間最初來自PHP。 – 2012-02-28 00:23:31

回答

0

哦,剛剛看到我對此有點慢,但它似乎工作,所以我會加入它無論如何。 :)

function parseTime() { 
    var timeLeftStr; 
    var timeLeft = 0; 

    timeLeftStr = document.getElementById("timeleft").innerHTML; 

    timeLeftStr.replace(/(\d+):(\d+):(\d+)/, function() { 
     for (var i = 1; i < arguments.length - 2; i++) { 
      // Convert to ms 
      timeLeft += arguments[i] * Math.pow(60, 3 - i) * 1000; 
     } 
    }); 

    countdown(new Date(timeLeft)); 
} 

function countdown(timeLeft) { 
    var hours = timeLeft.getUTCHours(); 
    var minutes = timeLeft.getUTCMinutes(); 
    var seconds = timeLeft.getUTCSeconds(); 

    if (timeLeft.valueOf() == 0) { 
     document.getElementById("timeleft").innerHTML = ""; 
     window.location = 'test.php?pageid=' + getURLParam("pageid"); 
     return false; 
    } else { 
     document.getElementById("timeleft").innerHTML = 
      (hours == 0 ? "" : hours + (hours == 1 ? " hour" : " hours")) + 
      (minutes == 0 ? "" : (hours ? (seconds ? ", " : " and ") : " ") + minutes + (minutes == 1 ? " minute" : " minutes")) + 
      (seconds == 0 ? "" : (hours || minutes ? " and " : "") + seconds + (seconds == 1 ? " second" : " seconds")); 

     setTimeout(function() { countdown(new Date(timeLeft - 1000)); }, 1000); 
    } 
} 

window.onload = parseTime; 

編輯刪除skip

編輯2以除去正則表達式開始&端檢查,加入,and支持。

編輯3使用getUTCHours等,所以它可以在其他時區(oops)工作。

+0

謝謝你的幫助。我用我的另一個腳本交換它,計時器保持不動。 – 2012-02-28 00:22:33

+0

@SamiDzHamida這可能是因爲'skip'。我不知道'skip'在做什麼,但是因爲它在原來的我已經離開了它。如果你不需要它,只要刪除'if'(當然調整'else')。當我寫這篇文章的時候,我刪除了它。 – meloncholy 2012-02-28 00:26:00

+0

好的,謝謝。爲了幫助更多,我添加了一個關於PHP端看起來像什麼的編輯。 – 2012-02-28 00:35:15

0

而是這行:

var timeleft = ((hours < 10) ? "0" : "") + hours + ":" + ((minutes < 10) ? "0" : "") + minutes + ":" + ((seconds < 10) ? "0" : "") + seconds; 

說,像這樣一個基本的解決方案:

var timeleft = (hours > 0 ? hours + " hours " : "") 
      + (minutes > 0 ? minutes + " minutes " : "") 
      + (seconds > 0 ? seconds + " seconds " : ""); 

或全部:

var timeleft = ""; 
if (hours > 0) 
    timeleft += hours + (hours > 1 ? " hours" : " hour"); 
if (minutes > 0) { 
    if (hours > 0) 
     timeleft += seconds > 0 ? ", " : " and "; 
    timeleft += minutes + (minutes > 1 ? " minutes" : " minute"); 
} 
if (seconds > 0) { 
    if (timeleft != "") 
     timeleft += " and "; 
    timeleft += seconds + (seconds > 1 ? " seconds" : " second"); 
} 
+0

謝謝你,我會嘗試,現在 – 2012-02-28 00:10:31

+0

我改了行爲全面之一,但它似乎並不奏效。 – 2012-02-28 00:16:31

0

好吧,我做了這現在,它的工作原理(我在Opera中測試):

Date.prototype.toTimeString = function() 
{ 
    var toReturnTime = new Array(); 
    var times = [this.getHours(), this.getMinutes(), this.getSeconds()]; 
    var times_names = [' hour', ' minute', ' second']; 
    var tmp = null; 
    for (var i=0; i<times.length; i++) 
    { 
    tmp = times[i]; 
    if (tmp > 0) 
    { 
     toReturnTime.push(tmp + times_names[i] + (tmp == 1 ? '' : 's')); 
     toReturnTime.push(', '); 
    } 
    } 
    if (toReturnTime.length/2 >= 2) 
    { 
    toReturnTime.pop(); 
    tmp = toReturnTime.pop(); 
    toReturnTime.pop(); 
    toReturnTime.push(' and ' + tmp); 
    } 
    else 
    toReturnTime.pop(); 
    return toReturnTime.join(''); 
} 


var date, timeleft; 

function secondCountdown() 
{ 
    date.setSeconds(date.getSeconds() - 1); 
    timeleft.innerHTML = date.toTimeString(); 
    if ((date.getHours() + date.getMinutes() + date.getSeconds()) > 0) 
    setTimeout("secondCountdown()",1000); 
    else 
    timeleft.innerHTML = 'ITS OVER'; 
} 

function init() 
{ 
    timeleft = document.getElementById("timeleft"); 
    var time_split = timeleft.innerHTML.split(":"); 
    date = new Date(0, 0, 0, parseInt(time_split[0]), parseInt(time_split[1]), parseInt(time_split[2])); 
    secondCountdown(); 
} 
window.onload = init; 
+0

謝謝你的幫助。它似乎並沒有爲我工作:( – 2012-02-28 00:26:21