JavaScript 倒计时时钟
<script>
var interval;
var minutes = 1;
var seconds = 5;
window.onload = function() {
countdown('countdown');
}
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
el.innerHTML = "countdown's over!";
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
</script>
这是一个很好的倒计时时钟,我想在数据列表中显示时间我该怎么做? 就像这个网站 www.1buy1.co.il
<script>
var interval;
var minutes = 1;
var seconds = 5;
window.onload = function() {
countdown('countdown');
}
function countdown(element) {
interval = setInterval(function() {
var el = document.getElementById(element);
if(seconds == 0) {
if(minutes == 0) {
el.innerHTML = "countdown's over!";
clearInterval(interval);
return;
} else {
minutes--;
seconds = 60;
}
}
if(minutes > 0) {
var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
} else {
var minute_text = '';
}
var second_text = seconds > 1 ? 'seconds' : 'second';
el.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
seconds--;
}, 1000);
}
</script>
this is a good countdown clock and i want to show the time in datalist how do i do it?
like in this site www.1buy1.co.il
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在这里看到的答案效果很好,但并不完全严格。尽管 setInterval() 似乎是正确使用的函数,但随着时间的推移,它会出现轻微的“漂移”。此外,如果其他一些 JavaScript 函数需要一秒或更长时间才能执行,那么您的倒计时时钟可能会停止并显示错误的时间。
尽管这些情况不太可能发生,但提高精度实际上并不困难。您想要的是一个能够将时钟从不准确的情况中拉回来的计时器。您需要根据系统时钟计算时间,而不是根据时间间隔的频率来计算,为此,您必须放弃 setInterval(),转而使用一系列 setTimeout() 调用。下面的代码展示了如何操作。
尝试一下:http://jsfiddle.net/mrwilk/qVuHW
如果您的时钟碰巧与秒数非常接近系统时钟时,由于在一秒间隔之前或之后不久接收到超时事件,您的倒计时器可能会出现“错过节拍”的情况。解决方案是在半秒内调整您的事件;也就是说,系统时钟的秒拍之间的中间位置。这就是代码中 500 的作用,其中 500 毫秒 = ½ 秒。
唯一值得注意的是,这里的代码显示小时以及分钟和秒;即,HH:MM:SS。从毫秒计算小时、分钟和秒并不困难,但有点尴尬,最简单的方法是让 Date 对象为您完成这项工作。
The answers I see here work pretty well but are not entirely rigorous. Although setInterval() seems like the right function to use, it suffers from a slight "drift" over time. Also, if some other JavaScript function takes a second or more to execute then your countdown clock can get stalled and show the wrong time.
Although these occurrences might be unlikely, greater precision is really not more difficult. What you want is a timer that pulls the clock back from any inaccuracy. You'll need to calculate time off the system clock rather than from the frequency of time intervals, and for this you'll have to give up setInterval() in favor of a series of setTimeout() calls. The following code shows how.
Try it: http://jsfiddle.net/mrwilk/qVuHW
If your clock should by chance align very closely to the seconds of the system clock, your countdown timer can appear to "miss beats" due to receiving timeout events just slightly before or just slightly after a one-second interval. The solution is to align your events on the half-second; that is, midway between the beats of the seconds of the system clock. That what the 500's in the code do, where 500ms = ½sec.
The only other matter worth noting is that the code here displays hours as well as minutes and seconds; that is, HH:MM:SS. Computing hours, minutes and seconds from milliseconds is not difficult but is a bit awkward, and it's simplest to let the Date object do this work for you.
我已经清理了上面的代码并使其格式像您的示例一样。它还删除了全局变量并允许您创建多个计时器。
这里有一个实时链接 http://jsfiddle.net/Apnu2/6/
I've cleaned up the above code and made it format like your example. It also removed the global variables and allows you to create multiple timers.
There's a live link here http://jsfiddle.net/Apnu2/6/
脚本中的
element
是您传递给函数的标记的 id。因此
countdown('div1')
将在 id="div1" 的标记中显示计时器,但是您的特定计时器不允许多个实例,并使用名为interval 的全局变量。
在服务器上生成 JSON,剩下的事情会自行处理,
这是一个更精确的 OO 版本,因为它只计算结束时间,而不是依赖于间隔,如果浏览器繁忙,间隔就会阻塞: http://jsbin.com/ujuzo5/edit
这是我在 SO 的好人帮助下的原始解决方案: 关闭损坏 - 请帮我修复
The
element
in the script is the id of the tag you pass to the function.So
countdown('div1')
will show a timer in a tag with id="div1"However your particular timer does not allow multiple instances and uses a global var called interval.
Generate the JSON on the server and the rest takes care of itself
HERE is an OO version that is more precise since it is only calculating the end time instead of relying on interval which will block if the browser is busy: http://jsbin.com/ujuzo5/edit
And here is my original solution with the help of the good folks at SO here: Broken closure - please help me fix it