JQuery倒数计时器,毫秒(JQuery countdown timer with milliseconds)

编程入门 行业动态 更新时间:2024-10-12 10:19:17
JQuery倒数计时器,毫秒(JQuery countdown timer with milliseconds)

我有一个非常基本的轻量级功能,从30秒倒计时。 我一直试图增加毫秒,但我似乎无法让它正常工作。

var count = 30; var counter = setInterval(timer, 1000); //1000 will run it every 1 second function timer() { if (count <= 0) { clearInterval(counter); return; } count = count - 1; document.getElementById("timer").innerHTML = count + " secs"; // watch for spelling }

I have a very basic lightweight function that counts down from 30 seconds. I have been trying to add milliseconds to it but I can't seem to get it to work correctly.

var count = 30; var counter = setInterval(timer, 1000); //1000 will run it every 1 second function timer() { if (count <= 0) { clearInterval(counter); return; } count = count - 1; document.getElementById("timer").innerHTML = count + " secs"; // watch for spelling }

最满意答案

试试这种方式 。 秒表无论如何只能计数百分之一秒。

var count = 3000; var counter = setInterval(timer, 10); //10 will run it every 100th of a second function timer() { if (count <= 0) { clearInterval(counter); return; } count--; document.getElementById("timer").innerHTML=count /100+ " secs"; }

只是为了更好的格式和测试 :

HTML

<span id="timer"></span> <button id="start">start</button> <button id="stop">stop</button> <button id="reset">reset</button>

使用Javascript

var initial = 30000; var count = initial; var counter; //10 will run it every 100th of a second function timer() { if (count <= 0) { clearInterval(counter); return; } count--; displayCount(count); } function displayCount(count) { var res = count / 100; document.getElementById("timer").innerHTML = res.toPrecision(count.toString().length) + " secs"; } $('#start').on('click', function () { clearInterval(counter); counter = setInterval(timer, 10); }); $('#stop').on('click', function () { clearInterval(counter); }); $('#reset').on('click', function () { clearInterval(counter); count = initial; displayCount(count); }); displayCount(initial);

编辑:

最初的问题是想弄清楚如何让显示器像秒表一样,而且我知道很少有实际的计数为毫秒。 这就是说, 这是一个可行的解决方案。 它依赖于尽可能快的更新,并使我们上次更新和我们当前的更新保持准确。

var initial = 300000; var count = initial; var counter; //10 will run it every 100th of a second var initialMillis; function timer() { if (count <= 0) { clearInterval(counter); return; } var current = Date.now(); count = count - (current - initialMillis); initialMillis = current; displayCount(count); } function displayCount(count) { var res = count / 1000; document.getElementById("timer").innerHTML = res.toPrecision(count.toString().length) + " secs"; } $('#start').on('click', function () { clearInterval(counter); initialMillis = Date.now(); counter = setInterval(timer, 1); }); $('#stop').on('click', function () { clearInterval(counter); }); $('#reset').on('click', function () { clearInterval(counter); count = initial; displayCount(count); }); displayCount(initial);

Try it this way. Stopwatches only count hundredths of seconds anyway.

var count = 3000; var counter = setInterval(timer, 10); //10 will run it every 100th of a second function timer() { if (count <= 0) { clearInterval(counter); return; } count--; document.getElementById("timer").innerHTML=count /100+ " secs"; }

Just for better formatting and testing:

HTML

<span id="timer"></span> <button id="start">start</button> <button id="stop">stop</button> <button id="reset">reset</button>

Javascript

var initial = 30000; var count = initial; var counter; //10 will run it every 100th of a second function timer() { if (count <= 0) { clearInterval(counter); return; } count--; displayCount(count); } function displayCount(count) { var res = count / 100; document.getElementById("timer").innerHTML = res.toPrecision(count.toString().length) + " secs"; } $('#start').on('click', function () { clearInterval(counter); counter = setInterval(timer, 10); }); $('#stop').on('click', function () { clearInterval(counter); }); $('#reset').on('click', function () { clearInterval(counter); count = initial; displayCount(count); }); displayCount(initial);

EDIT:

The original question was trying to figure out how to make a display like a stopwatch, and I know very few that actually count milliseconds. That being said, here is a possible solution to do that. It relies on updating as fast as possible, and getting the difference between our last update and our current one to remain accurate.

var initial = 300000; var count = initial; var counter; //10 will run it every 100th of a second var initialMillis; function timer() { if (count <= 0) { clearInterval(counter); return; } var current = Date.now(); count = count - (current - initialMillis); initialMillis = current; displayCount(count); } function displayCount(count) { var res = count / 1000; document.getElementById("timer").innerHTML = res.toPrecision(count.toString().length) + " secs"; } $('#start').on('click', function () { clearInterval(counter); initialMillis = Date.now(); counter = setInterval(timer, 1); }); $('#stop').on('click', function () { clearInterval(counter); }); $('#reset').on('click', function () { clearInterval(counter); count = initial; displayCount(count); }); displayCount(initial);

更多推荐

本文发布于:2023-08-03 09:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   倒数   JQuery   milliseconds   timer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!