2013-06-06 3 views
0

이 스크립트 (신용 http://www.rmkwebdesign.com/Countdown_Timers/Repeating_Daily_Timer.html)는 기본적으로 자바 스크립트 일일 카운트 다운입니다. getDate 변수를 getUTCDate로 변경했지만 여전히 UTC가 아니며 클라이언트 시계에 따라 다릅니다. 이 문제를 어떻게 해결하고 클라이언트 컴퓨터 시간에 의존하지 않도록하여 클라이언트 컴퓨터 시간이 잘못되면 카운트 다운이 잘못 표시되지 않습니까 ?? 감사. 내가 생각할 수Javascript 타이머는 UTC에 따라 컴퓨터 시간에 좌우됩니까?

<script type="text/javascript"> 

var current = "Test"; 

var startHour = 0; 

var startMinute = 0; 

var endHour = 23; 

var endMinute = 55; 

var tz = 0; 

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); 

if (startMinute < 10){startMinute = "0" + startMinute;} 

function countdown() 
{ 
var today=new Date(); 
var todayy=today.getYear(); 
if (todayy < 1000) 
{todayy += 1900;} 

var todaym = today.getUTCMonth(); 
var todayd = today.getUTCDate(); 
var todayh = today.getUTCHours(); 
var todaymin = today.getUTCMinutes(); 
var todaysec = today.getUTCSeconds(); 

// add a zero in front of numbers<10 
todaymin = checkTime(todaymin); 
todaysec = checkTime(todaysec); 

var startString = parseInt(startHour + "" + startMinute + "00"); 

var nowString1 = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec; 
var nowString = Date.parse(nowString1) + (tz*1000*60*60); 
var nowTime = parseInt(todayh + "" + todaymin + "" + todaysec); 

var endString1 = (montharray[todaym] + " " + todayd + ", " + todayy + " " + endHour + ":" + endMinute); 
var endString = Date.parse(endString1) - (today.getTimezoneOffset() * (1000*60)); 

var dd = endString - nowString; 
var dday = Math.floor(dd/(60*60*1000*24)*1); 
var dhour = Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1); 
var dmin = Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1); 
var dsec = Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1); 

if ((dhour <= 0 && dmin <= 0 && dsec <= 0) || (nowTime < startString)) 
{ 
    document.getElementById('count2').innerHTML = current; 
    document.getElementById('count2').style.display = "inline"; 
    document.getElementById('count2').style.width = "390px"; 
    document.getElementById('dhour').style.display = "none"; 
    document.getElementById('dmin').style.display = "none"; 
    document.getElementById('dsec').style.display = "none"; 
    document.getElementById('hours').style.display = "none"; 
    document.getElementById('minutes').style.display = "none"; 
    document.getElementById('seconds').style.display = "none"; 
    setTimeout("countdown()",500); 
    return; 
} 

else 
{ 
    document.getElementById('count2').style.display = "none"; 
    document.getElementById('dhour').style.display = "inline"; 
    document.getElementById('dmin').style.display = "inline"; 
    document.getElementById('dsec').style.display = "inline"; 
    document.getElementById('hours').style.display = "inline"; 
    document.getElementById('minutes').style.display = "inline"; 
    document.getElementById('seconds').style.display = "inline"; 
    document.getElementById('dhour').innerHTML = dhour; 
    document.getElementById('dmin').innerHTML = dmin; 
    document.getElementById('dsec').innerHTML = dsec; 
    setTimeout("countdown()",500); 
} 

if (dhour < 2 || (dhour == 2 && dmin == 0 && dsec == 0)) 
{ 
    document.getElementById('dhour').style.color = "yellow"; 
    document.getElementById('dmin').style.color = "yellow"; 
    document.getElementById('dsec').style.color = "yellow"; 
} 

if (dhour < 1 || (dhour == 1 && dmin == 0 && dsec == 0)) 
{ 
    document.getElementById('dhour').style.color = "red"; 
    document.getElementById('dmin').style.color = "red"; 
    document.getElementById('dsec').style.color = "red"; 
} 
} 

function checkTime(i) 
{ 
if (i<10) 
    { 
    i="0" + i; 
    } 
return i; 
} 
</script> 

답변

0

첫 번째 솔루션은 클라이언트에게 타이머가 대신 타임 스탬프의 계속 시간 (초) 양을 보내,하지만 그것은 인해 HTTP에 부정확 할 수 있습니다 :

는 여기에 자바 스크립트 코드입니다 전송 대기 시간.

관련 문제