2013-06-06 35 views
0

所以这个脚本(功劳http://www.rmkwebdesign.com/Countdown_Timers/Repeating_Daily_Timer.html),基本上是一个Javascript每日倒计时。我将getDate变量更改为getUTCDate,但它仍不是UTC,取决于客户端时钟。我如何解决这个问题,并使其不依赖于客户端计算机的时间,如果客户端计算机时间错误,倒计时不会显示错误?谢谢。JavaScript定时器到UTC取决于计算机时间?

这里的javascript代码:

<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传输延迟。