一个简单的 JavaScript 倒计时器的代码?
我想使用一个简单的倒计时器,从函数运行时的 30 秒开始,到 0 结束。没有毫秒。 如何对其进行编码?
I want to use a simple countdown timer starting at 30 seconds from when the function is run and ending at 0. No milliseconds. How can it be coded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
基于@Layton Everson 提出的解决方案,我开发了一个包括小时、分钟和秒的计数器:
Based on the solution presented by @Layton Everson I developed a counter including hours, minutes and seconds:
我的解决方案适用于 MySQL 日期时间格式并提供回调函数。 关于完成。
免责声明:仅适用于分钟和秒,因为这是我需要的。
My solution works with MySQL date time formats and provides a callback function. on complition.
Disclaimer: works only with minutes and seconds, as this is what I needed.
要使计时器的代码出现在段落中(或页面上的任何其他位置),只需将行:
放在您希望秒数出现的位置即可。 然后在您的
timer()
函数中插入以下行,如下所示:To make the code for the timer appear in a paragraph (or anywhere else on the page), just put the line:
where you want the seconds to appear. Then insert the following line in your
timer()
function, so it looks like this:我前段时间写了这个脚本:
用法:
I wrote this script some time ago:
Usage:
到目前为止,答案似乎依赖于立即运行的代码。 如果你设置一个1000ms的定时器,它实际上会在1008左右。
您应该这样做:
要使用,请致电:
So far the answers seem to rely on code being run instantly. If you set a timer for 1000ms, it will actually be around 1008 instead.
Here is how you should do it:
To use, call:
如果有人需要几分钟和几秒钟的话,这是另一个:
Here is another one if anyone needs one for minutes and seconds:
刚刚修改了@ClickUpvote的答案:
您可以使用IIFE(立即调用函数表达式) 和递归使其更容易一些:
Just modified @ClickUpvote's answer:
You can use IIFE (Immediately Invoked Function Expression) and recursion to make it a little bit more easier:
扩展已接受的答案,您的机器将进入睡眠状态等可能会延迟计时器的工作。 您可以获得真实的时间,但需要进行一些处理。 这将给出真实的剩余时间。
Expanding upon the accepted answer, your machine going to sleep, etc. may delay the timer from working. You can get a true time, at the cost of a little processing. This will give a true time left.
为了性能,我们现在可以安全地使用 requestAnimationFrame 用于快速循环,而不是 setInterval/setTimeout。
使用 setInterval/setTimeout 时,如果循环任务花费的时间超过间隔时间,浏览器将简单地延长间隔循环,以继续完整渲染。 这正在造成问题。 setInterval/setTimeout 过载几分钟后,这可能会冻结选项卡、浏览器或整个计算机。
互联网设备的性能多种多样,因此很难以毫秒为单位硬编码固定的间隔时间!
使用日期对象来比较开始日期纪元和当前。 这比其他一切都快得多,浏览器将以稳定的 60FPS 处理一切(1000 / 60 = 每帧 16.66 毫秒) -眨眼四分之一次 - 如果任务在循环中如果需要更多,浏览器将放弃一些重绘。
这在我们的眼睛注意到之前留出了余量(人类 = 24FPS => 1000 / 24 = 41.66 毫秒每帧 = 流畅的动画!)
https://caniuse.com/#search=requestAnimationFrame
For the sake of performances, we can now safely use requestAnimationFrame for fast looping, instead of setInterval/setTimeout.
When using setInterval/setTimeout, if a loop task is taking more time than the interval, the browser will simply extend the interval loop, to continue the full rendering. This is creating issues. After minutes of setInterval/setTimeout overload, this can freeze the tab, the browser or the whole computer.
Internet devices have a wide range of performances, so it's quite impossible to hardcode a fixed interval time in milliseconds!
Using the Date object, to compare the start Date Epoch and the current. This is way faster than everything else, the browser will take care of everything, at a steady 60FPS (1000 / 60 = 16.66ms by frame) -a quarter of an eye blink- and if the task in the loop is requiring more than that, the browser will drop some repaints.
This allow a margin before our eyes are noticing (Human = 24FPS => 1000 / 24 = 41.66ms by frame = fluid animation!)
https://caniuse.com/#search=requestAnimationFrame
使用纯JS可以进行如下操作。 您只需向该函数提供秒数,它就会完成其余的工作。
You can do as follows with pure JS. You just need to provide the function with the number of seconds and it will do the rest.