随着 JavaScript 代码运行更新屏幕计数
嘿,我刚刚开始学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你需要给 JS 一点时间来更新 DOM。现在它有其他优先事项(检查数字)。
替换
为
应该做的。
You need to give JS a bit time to update the DOM. Now it has other priorities (checking the numbers).
Replacing
by
should do.
首先考虑优化代码。如果您可以使其速度更快,则无需处理长时间运行的作业。你需要这么频繁地更新 DOM 吗?那么每
n
次迭代又如何呢?如果您必须有长时间运行的代码:
这也可以防止浏览器因运行时间过长而终止脚本。
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:
This also prevents the browser from killing the script for being too long-running.
在函数内定义命名函数似乎有点奇怪。您可以尝试执行以下操作:
我不太确定是什么导致它挂起。
Seems a little odd to be defining named function within a function. You might try instead doing something like:
I'm not quite sure what is causing it to hang though.
最简单的方法可能是在递归调用周围添加 setTimeout 进行检查,以便允许浏览器在中间时间内工作。下面我将超时设置为 100 毫秒,这意味着每秒更新 10 次。
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.