随着 JavaScript 代码运行更新屏幕计数

发布于 2024-08-10 23:02:13 字数 1011 浏览 2 评论 0原文

嘿,我刚刚开始学习 JavaScript,我正在制作一个生成两个数字的小脚本,第一个数字保持不变,但如果第二个数字与第一个数字不匹配,则重新生成第二个数字。

这是我的脚本:

function randomNumberMatcher(){
    $(document).ready(function(){
        var number1 = Math.floor(1000000*Math.random());
        var number2 = Math.floor(1000000*Math.random());
        var count = 0;
        $("#box").append("Number to match:[" + number1 + "]<br /><span id='count'></span>");
        function newNumber(){
            number2 = Math.floor(1000000*Math.random())
            count ++;
            $("#count").html("Number of tries:[" + count + "]<br /><br />");
            $("#box").append(number2 + "<br />");
            check();
        }
        function check(){
            if(number2 != number1){
                newNumber();
            }
        }
        check();
    });
};

当我运行脚本时,它所做的只是挂起直到完成,然后将数据打印到屏幕上,但这不是我想要做的,我想要的是它打印数据实时传输到屏幕,这样我就可以看到它生成的不同数字一一出现在屏幕上。

我该如何让它做到这一点?

注意:我也在使用 jQuery 库。

Hey, I've just started learning JavaScript and I'm making a little script that generates two numbers, the first number stays the same but the second number gets regenerated if it doesn't match the first number.

Here is my script:

function randomNumberMatcher(){
    $(document).ready(function(){
        var number1 = Math.floor(1000000*Math.random());
        var number2 = Math.floor(1000000*Math.random());
        var count = 0;
        $("#box").append("Number to match:[" + number1 + "]<br /><span id='count'></span>");
        function newNumber(){
            number2 = Math.floor(1000000*Math.random())
            count ++;
            $("#count").html("Number of tries:[" + count + "]<br /><br />");
            $("#box").append(number2 + "<br />");
            check();
        }
        function check(){
            if(number2 != number1){
                newNumber();
            }
        }
        check();
    });
};

At the moment when i run the script all it does is hang until it has finished and then it prints the data to the screen, this however is not what I intended it to do, what I want is it print the data to the screen in real time so that I can see the different numbers it is generating appear on the screen one by one.

How would I make it do this?

note: I'm also using the jQuery library.

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

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

发布评论

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

评论(4

2024-08-17 23:02:13

你需要给 JS 一点时间来更新 DOM。现在它有其他优先事项(检查数字)。

替换

if(number2 != number1){
    newNumber();
}

if(number2 != number1){
    setTimeout(newNumber, 1);
}

应该做的。

You need to give JS a bit time to update the DOM. Now it has other priorities (checking the numbers).

Replacing

if(number2 != number1){
    newNumber();
}

by

if(number2 != number1){
    setTimeout(newNumber, 1);
}

should do.

ˉ厌 2024-08-17 23:02:13

首先考虑优化代码。如果您可以使其速度更快,则无需处理长时间运行的作业。你需要这么频繁地更新 DOM 吗?那么每 n 次迭代又如何呢?

如果您必须有长时间运行的代码:

  1. 将工作分成更小的部分。
  2. 每件作品后更新屏幕。
  3. 使用 window.setTimeout 在(非常)短暂的延迟后开始下一段。

这也可以防止浏览器因运行时间过长而终止脚本。

First consider optimizing the code. There's no need to handle a long-running job if you can make it faster. Do you need to update the DOM so often? What about every n iterations instead?

If you must have long-running code:

  1. Break the job up into smaller pieces.
  2. Update the screen after each piece.
  3. Use window.setTimeout to start the next piece after a (very) short delay.

This also prevents the browser from killing the script for being too long-running.

忆沫 2024-08-17 23:02:13

在函数内定义命名函数似乎有点奇怪。您可以尝试执行以下操作:

var newNumber = function () {...}
var check = function () {...}

我不太确定是什么导致它挂起。

Seems a little odd to be defining named function within a function. You might try instead doing something like:

var newNumber = function () {...}
var check = function () {...}

I'm not quite sure what is causing it to hang though.

忘东忘西忘不掉你 2024-08-17 23:02:13

最简单的方法可能是在递归调用周围添加 setTimeout 进行检查,以便允许浏览器在中间时间内工作。下面我将超时设置为 100 毫秒,这意味着每秒更新 10 次。

function randomNumberMatcher(){
    $(document).ready(function(){
        var number1 = Math.floor(1000000*Math.random());
        var number2 = Math.floor(1000000*Math.random());
        var count = 0;
        $("#box").append("Number to match:[" + number1 + "]<br /><span id='count'></span>");
        function newNumber(){
                number2 = Math.floor(1000000*Math.random())
                count ++;
                $("#count").html("Number of tries:[" + count + "]<br /><br />");
                $("#box").append(number2 + "<br />");
                window.setTimeout(check,100);
        }
        function check(){
                if(number2 != number1){
                        newNumber();
                }
        }
        check();
    });
};

The easiest way is probably to add a setTimeout around your recursive call to check so that the browser can be allowed to work for the intervening time. Below I've set the timeout to 100ms which means that it will update 10 times per second.

function randomNumberMatcher(){
    $(document).ready(function(){
        var number1 = Math.floor(1000000*Math.random());
        var number2 = Math.floor(1000000*Math.random());
        var count = 0;
        $("#box").append("Number to match:[" + number1 + "]<br /><span id='count'></span>");
        function newNumber(){
                number2 = Math.floor(1000000*Math.random())
                count ++;
                $("#count").html("Number of tries:[" + count + "]<br /><br />");
                $("#box").append(number2 + "<br />");
                window.setTimeout(check,100);
        }
        function check(){
                if(number2 != number1){
                        newNumber();
                }
        }
        check();
    });
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文