JavaScript Countdown计时器每天到特定时间 [英] JavaScript Countdown Timer to specific time everyday
问题描述
我目前正在开发一个标题为倒数计时器的网站: http://iphone.myhandykey.com/
目前的计时器只有12小时+几分钟..我想要的是倒数计时器将显示剩余时间直到晚上11点的时区现在的访客。那可能吗?谢谢!
这是JavaScript:
函数startTimer(持续时间,显示){
var timer =持续时间,小时,分钟,秒;
setInterval(function(){
hours = parseInt(((timer / 60)/ 60)%60,10);
分钟= parseInt((定时器/ 60)%60,10);
秒= parseInt(定时器%60,10);
小时=小时< 10?0+小时:小时;
分钟=分钟< 10?0+分钟:分钟;
秒=秒< 10?0+秒:秒;
display.textContent =小时+:+分钟+:+秒;
if(--timer< ; 0){
计时器=持续时间;
}
},1000);
}
window.onload = function(){
var onehour = 60 * 600 * 1.231,
display = document.querySelector('#time');
startTimer(一小时,显示);
};
这是HTML:
< span id = time>< / span>
编辑:如果访客的当前时间是例如晚上11点40分,它应该显示23小时&剩下20分钟..
(function(){var start = new Date; start.setHours(23,0,0); // 11pm function pad(num){return(0+ parseInt(num))。substr(-2);} function tick(){var now =新的日期; if(现在>开始){//太晚了,去明天start.setDate(start.getDate()+ 1);} var remaining =((start - now)/ 1000); var hh = pad ((保持/ 60/60)%60); var mm = pad((保持/ 60)%60); var ss = pad(保留%60); document.getElementById('time')。innerHTML = hh + :+ mm +:+ ss; setTimeout(tick,1000);} document.addEventListener('DOMContentLoaded',tick);})();
< pre class =snippet-code-html lang-html prettyprint-override> 仅< span id ='time'>< / span>离开!
I am currently developing a website with a countdown timer at the headline: http://iphone.myhandykey.com/
The current timer is just 12hrs + few mins.. What I would like is the countdown timer will show the time remaining until 11PM on the Time Zone of the current visitor. Is that possible? Thanks!
Here is the JavaScript:
function startTimer(duration, display) {
var timer = duration, hours, minutes, seconds;
setInterval(function () {
hours = parseInt(((timer / 60) / 60 ) % 60, 10);
minutes = parseInt((timer / 60)%60, 10);
seconds = parseInt(timer % 60, 10);
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = hours + ":" + minutes + ":" + seconds;
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
window.onload = function () {
var onehour = 60 * 600 * 1.231,
display = document.querySelector('#time');
startTimer(onehour, display);
};
Here is the HTML:
<span id=time></span>
EDIT: If the visitor's current time is for example 11:40pm, It should display 23hrs & 20mins left..
(function() {
var start = new Date;
start.setHours(23, 0, 0); // 11pm
function pad(num) {
return ("0" + parseInt(num)).substr(-2);
}
function tick() {
var now = new Date;
if (now > start) { // too late, go to tomorrow
start.setDate(start.getDate() + 1);
}
var remain = ((start - now) / 1000);
var hh = pad((remain / 60 / 60) % 60);
var mm = pad((remain / 60) % 60);
var ss = pad(remain % 60);
document.getElementById('time').innerHTML =
hh + ":" + mm + ":" + ss;
setTimeout(tick, 1000);
}
document.addEventListener('DOMContentLoaded', tick);
})();
Only <span id='time'></span> left!
这篇关于JavaScript Countdown计时器每天到特定时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!