2011-08-28 2 views
0

매 시간마다 H- "시간 남음", 매분마다 M- "분 남음", 초 단위로 같은 시간을 표시하는이 타이머를 만들었습니다. 타임 스탬프 값에 따라 다릅니다. 당신은 단지 하나의 타이머를 필요새로 고침 타이머에서 H- "시간"에서 M- "분"으로 전환

window.onload = function() { 

     var actual_timestamp = Math.round((new Date()).getTime()/1000); 
     var time_remaining=<?php echo $vote_time_limit ?> - actual_timestamp; 
     var spanElt = document.getElementById('timer'); 
     var h,m,s;  

    setInterval(function() { 

     //If time_remaining is between 24 and 1h, it decreases every hours 
     if(time_remaining<=86400 && time_remaining>=3600){ 

      h=Math.floor(time_remaining/3600 % 24); 
      spanElt.innerHTML="H-"+h--; 
      time_remaining--; 
     } 

    } , 3600000); 


    setInterval(function() { 

     //If time_remaining is between 1h and 1min, it decreases every minutes 
     if(time_remaining<3600 && time_remaining>=60){ 

      m=Math.floor(time_remaining/60 % 60); 
      spanElt.innerHTML="M-"+m--; 
      time_remaining--; 
      } 

    } , 60000); 

    setInterval(function() { 


     //If it is between 60sec and 0sec, it decreases every seconds 
     if(time_remaining<60 && time_remaining>0){ 

       s=Math.floor(time_remaining % 60); 
       spanElt.innerHTML="S-"+s--; 
       time_remaining--; 

     //If time_remaining =0, it takes the value of the cycle time 
     }else if(time_remaining==0){ 

       time_remaining=86400; //24h 
     } 

    } , 1000);   

    }; 

답변

1

:

당신이 내 스크립트를 개선하는 방법과 시간에서 스위치 타이머 페이지를 새로 고침없이 초 하나에 분 하나와 분 한에서 사물을 볼 수행 시계를 적절한 값으로 설정하십시오.

setInterval 필요한 간격 후에 가능한 한 빨리 실행 - 시스템이 사용 중이면 조금 늦게 천천히 표류합니다. 을 사용하는 것이 더 좋습니다. setTimeout - 다음 에포크까지의 시간을 예상하고 다음 전체 간격 이후 약 20ms 후에 다시 실행합니다.

+0

필자의 스크립트가 나쁘다고 생각했습니다. (하나의 setInterval만으로도 충분했습니다.) 도움을 주셔서 감사합니다. – Anon

관련 문제