2012-02-27 3 views
0

기본적으로 나는 자바 스크립트를 잘하고 내 프로젝트에 대한 더 나은 카운트 다운 타이머를 원합니다. 내가 같이하기를 원하는자바 스크립트 PHP 좋은 카운트 다운 타이머

1시 25분 5초

:

1시간 25 분 5 초 순간 내 타이머는 다음과 같이 보입니다

하지만 나는 또한 그것이 영리하기를 바랍니다. 따라서 25 분이면

25 분으로 표시됩니다.


이 내 현재의 자바 스크립트 코드 : 제가 원하는 무엇을 출력 할 수 있도록

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

+0

글쎄 JS 스크립트에 스크립트를 원하지만 시간은 원래 PHP에서 온다. –

답변

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 지원을 추가 할 수 있습니다.

편집 3getUTCHours 등을 사용하면 다른 시간대 (oops)에서 작동합니다.

+0

도와 줘서 고맙습니다.나는 다른 스크립트와 그것을 바꿨고 타이머는 여전히 그대로 남아 있습니다. –

+0

@SamiDzHamida 그 이유는 '건너 뛰기'때문일 수 있습니다. 나는'skip'이 무엇을하고 있는지 전혀 알지 못합니다.하지만 원래 그대로 남아 있습니다. 필요없는 경우'if'를 삭제하십시오 (물론'else'를 조정하십시오). 이 글을 쓰면서 나는 그것을 제거했다. – meloncholy

+0

감사합니다. 더 많은 것을 돕기 위해 PHP 쪽의 모습을 편집했습니다. –

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

고마워, 지금 해보겠습니다. –

+0

전체 라인을 바꿨지 만 작동하지 않는 것 같습니다. –

0

좋아, 내가 만든 이 지금, 그리고 그것이 작동 (나는 오페라에서 테스트) :

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

도와 주셔서 감사합니다. 그것은 나를 위해 일하지 않는 것 같습니다 : ( –