下面是一些我快速编写的代码,以获得一个良好的毫秒倒计时。它显示新的一年前的毫秒数,无论2015年的时区是什么,都是大字体,然后在下面以小字体显示时区。
<!DOCTYPE html>
<html>
<head>
<title>Newyear Countdown</title>
<style>
h1 {
font: 25vw "Helvetica Neue";
margin: 0;
}
</style>
</head>
<body>
<h1 id="countdown">Loading</h1>
<p id="timezone">Loading</p>
<script>
var countdown = document.getElementById('countdown'),
timezoneText = document.getElementById('timezone'),
timezone = -12;
setInterval(function() {
countdown.removeChild(countdown.firstChild);
var text = new Date('2015-01-01T00:00:00Z').getTime() + 3600000 * timezone - new Date().getTime();
while (text < 0) text = new Date('2015-01-01T00:00:00Z').getTime() + 3600000 * ++timezone - new Date().getTime();
countdown.appendChild(document.createTextNode(text));
timezoneText.removeChild(timezoneText.firstChild);
timezoneText.appendChild(document.createTextNode('Milliseconds until newyear 2015 in timezone UTC' + (timezone > 0 ? '+' : '') + (timezone || '')));
}, 0);
</script>
</body>
</html>发布于 2015-01-01 06:11:22
我想你把时区处理错了:
这里是本地时区计时器的实现(因为这是您最初在问题中指定的)。
<!DOCTYPE html>
<html>
<head>
<title>Newyear Countdown</title>
<style>
h1 {
font: 25vw "Helvetica Neue";
margin: 0;
}
</style>
</head>
<body>
<h1 id="countdown">Loading</h1>
<p id="timezone">Loading</p>
<script>
function formatTimezone(offsetMinutes) {
if (offsetMinutes == 0) return 'UTC';
var hours = Math.floor(Math.abs(offsetMinutes / 60));
var minutes = Math.round(Math.abs(offsetMinutes) % 60);
var sign = (offsetMinutes > 0) ? '+' : '-';
return 'UTC' + sign +
('00' + hours).slice(-2) +
('00' + minutes).slice(-2);
}
function millisecondsUntil(target) {
return target - Date.now();
}
var countdown = document.getElementById('countdown'),
target = (new Date(2015, 0, 1)).getTime(),
timezoneText = document.getElementById('timezone'),
timezoneOffsetMinutes = -(new Date()).getTimezoneOffset();
timezoneText.removeChild(timezoneText.firstChild);
timezoneText.appendChild(document.createTextNode('Milliseconds until new year 2015 in timezone ' + formatTimezone(timezoneOffsetMinutes)));
setInterval(function() {
var text = millisecondsUntil(target);
countdown.removeChild(countdown.firstChild);
countdown.appendChild(document.createTextNode(text));
}, 1);
</script>
</body>
</html>https://codereview.stackexchange.com/questions/75404
复制相似问题