Firefox 10ms 间隔

发布于 2024-12-11 19:47:17 字数 2150 浏览 0 评论 0原文

所以我做了一个简单的计时器,格式如下:MM:SS.MS

你可以在这里查看它[已删除]
它在 chrome、IE 等中运行良好,但在 Firefox 中,秒数大约是原来的两倍。

我看了一些其他秒表,但我并没有放弃理解它们。 最好的方法是什么?现在我有一个 10 毫秒的间隔来生成计时器。

该功能看起来像这样,我希望它是可以理解的:

var state = false;
var msTimer = null;

var min = document.getElementById("min");
var sec = document.getElementById("sec");
var ms = document.getElementById("ms");

var minCount = 0;
var secCount = 0;
var msCount = 0;


document.onkeydown = function timer(e) {

if (!e) { e = window.event; }

if (e.keyCode == "32") {

    if (state == false) {
        state = true;

        min.innerHTML = "00";
        sec.innerHTML = "00";
        ms.innerHTML = "00";

        msTimer = window.setInterval(function() {
            if (msCount == 99) {

                msCount = 0;
                secCount++;

                // Minutes
                if (secCount == 60) {

                    secCount = 0;
                    minCount++;

                    if (minCount <= 9)
                    { min.innerHTML = "0" + minCount; }
                    else
                    { min.innerHTML = minCount; }
                }

                // Seconds
                if (secCount <= 9)
                { sec.innerHTML = "0" + secCount; }
                else
                { sec.innerHTML = secCount; }

            } else { msCount++; }

            // Miliseconds
            if (msCount <= 9)
            { ms.innerHTML = "0" + msCount; }
            else
            { ms.innerHTML = msCount; }

            // 1 Hour
            if (minCount == 60) {

                clearInterval(msTimer);

                min.innerHTML = "N0";
                sec.innerHTML = "00";
                ms.innerHTML = "0B";

                state = false;

                minCount = 0;
                secCount = 0;
                msCount = 0;
            }
        }, 10);

    } else if (state == true) {
        state = false;

        clearInterval(msTimer);

        minCount = 0;
        secCount = 0;
        msCount = 0;
    }
}

感谢您的任何建议:)

编辑:

顺便说一句,如果我从计时器中删除所有样式,它在 Firefox 中会更流畅。 但这不能是解决方案..

So I made a simple timer in a format like this: MM:SS.MS

You can view it here [removed]
It works fine in chrome, IE, etc. but in Firefox the seconds are like twice as long..

I took a look at some other stopwatches, but I don't quit understand them.
Whats the best way to do it ? Right now I have a 10ms interval which generates the timer.

The function looks like that, I hope it's understandable:

var state = false;
var msTimer = null;

var min = document.getElementById("min");
var sec = document.getElementById("sec");
var ms = document.getElementById("ms");

var minCount = 0;
var secCount = 0;
var msCount = 0;


document.onkeydown = function timer(e) {

if (!e) { e = window.event; }

if (e.keyCode == "32") {

    if (state == false) {
        state = true;

        min.innerHTML = "00";
        sec.innerHTML = "00";
        ms.innerHTML = "00";

        msTimer = window.setInterval(function() {
            if (msCount == 99) {

                msCount = 0;
                secCount++;

                // Minutes
                if (secCount == 60) {

                    secCount = 0;
                    minCount++;

                    if (minCount <= 9)
                    { min.innerHTML = "0" + minCount; }
                    else
                    { min.innerHTML = minCount; }
                }

                // Seconds
                if (secCount <= 9)
                { sec.innerHTML = "0" + secCount; }
                else
                { sec.innerHTML = secCount; }

            } else { msCount++; }

            // Miliseconds
            if (msCount <= 9)
            { ms.innerHTML = "0" + msCount; }
            else
            { ms.innerHTML = msCount; }

            // 1 Hour
            if (minCount == 60) {

                clearInterval(msTimer);

                min.innerHTML = "N0";
                sec.innerHTML = "00";
                ms.innerHTML = "0B";

                state = false;

                minCount = 0;
                secCount = 0;
                msCount = 0;
            }
        }, 10);

    } else if (state == true) {
        state = false;

        clearInterval(msTimer);

        minCount = 0;
        secCount = 0;
        msCount = 0;
    }
}

Thanks for any advices :)

Edit:

And btw, it's much smoother in firefox, if I remove all styles from the timer.
But that can't be the solution..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2024-12-18 19:47:18

您不应该依赖计时器的间隔正好 10 毫秒。如果您将每个计时器滴答视为刷新屏幕计时器的请求,但从系统时钟或类似的东西中获取测量,那就更好了。

然后,无论机器(和 JS 实现)有多慢或多忙,您总是会看到一个准确的计时器1 - 只是更新频率较低的计时器。


1 当然,这只会与系统时钟一样准确。如果 Javascript 可以访问高性能计时器,例如 .NET 的 Stopwatch 类或 Java 的 System.nanoTime()方法,这样会更好用。

You shouldn't be relying on the interval of the timer being exactly 10ms. It would be better if you viewed each timer tick as a request to refresh the on-screen timer, but take the measurements from the system clock or something like that.

Then however slow or busy the machine (and JS implementation) is, you'll always see an accurate timer1 - just one which updates less often.


1 This will only be as accurate as the system clock, of course. If Javascript has access to a high-performance timer like .NET's Stopwatch class or Java's System.nanoTime() method, that would be better to use.

ぃ弥猫深巷。 2024-12-18 19:47:18

无法保证 JavaScript 中的计时。 10 毫秒的间隔只会大约为 10 毫秒,很可能每次都会延迟一点点。在 Javascript 中创建计时器的正确方法是在启动计时器时保存起始时间戳,然后每隔 10 毫秒左右计算起始时间戳与当前时间之间的差异,并使用格式化值更新页面上的元素。

Timing in Javascript is not guaranteed. A 10ms interval will only ever be approximately 10ms, most likely it will be delayed a tiny bit each time. The correct way to do a timer in Javascript is to save a starting timestamp upon starting the timer, then each 10ms or so calculate the difference between the starting timestamp and the current time and update an element on the page with the formatted value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文