Javascript while循环使页面无法加载
当我将其放入标题中时:
<script>
function checkiffinished() {
var n = $('.result-row').length;
while (n!==3)
{
n = $('.result-row').length;
$("span").text("There are " + n + " divs.");
};
};
</script>
然后放入正文中,
<script>
checkiffinished();
</script>
然后页面崩溃。
该代码应该计算“结果行”类的 div 数量,并重复该操作,直到上述类的 div 数量为 3。
我怎样才能做到这一点而不会使页面崩溃?
编辑1:页面中的其他 jQuery 导致另一个事件上有 .result-row 元素,这就是为什么我需要它定期检查。
EDIT2:我试图与其他一些操作页面上元素的 jQuery 并行运行它。
When I put this in my header:
<script>
function checkiffinished() {
var n = $('.result-row').length;
while (n!==3)
{
n = $('.result-row').length;
$("span").text("There are " + n + " divs.");
};
};
</script>
Then in the body put
<script>
checkiffinished();
</script>
then the page crashes.
The code is supposed to count the number of divs with the "result-row" class and repeat that until the number of divs with aforementioned class is 3.
How can I make it so as such and not crash the page?
EDIT 1: other jQuery I have in the page causes there to be .result-row elements on another event, that's why I need it to check periodically.
EDIT2 : I'm trying to run this in parallel with some other jQuery that manipulates the elements on the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JavaScript 一次只能运行一段代码。所以你的 while 循环阻塞了所有 JavaScript 执行。因此,任何添加元素的代码都会被挂起——基本上永远挂起。
相反,您可能想要使用每秒检查一次的间隔: http://jsfiddle.net/pimvdb /sbs6m/1/。
JavaScript can only run one piece of code at a time. So your
while
loop is clogging up all JavaScript execution. As a result, any code that adds elements is suspended - basically forever.Instead, you might want to use an interval which checks e.g. each second: http://jsfiddle.net/pimvdb/sbs6m/1/.
你的问题是 n 没有减少。
pimvdb 的 Idea 是个好主意。这是一个包含回调的实现:
像这样使用它:
Your problem is that n is not decremented.
pimvdb's Idea is a good one. Here is an implementation that includes a callback:
Use it like this:
你的代码是一个无限循环 if
n !== 3
因为你在 while 循环中所做的任何事情都不会改变 n 的值,所以while
循环只是永远运行,等待n 的值变为 3。目前还不清楚为什么将其放入while
循环中。在我看来,它应该只是一个if
而不是while
。如果您要使用 while 循环,那么您必须有一些能够实际更改循环中 n 值的东西,以及确保您将达到 while 循环中的条件以防止无限循环的代码。
正如其他人所说,如果您想定期检查 3 的值,那么您需要使用 setTimeout 或 setInterval 重复执行此函数,而不仅仅是无限循环。但是,您不想经常执行它,否则可能会降低您的网站和浏览器中其他页面的性能。
实现此目的的最佳执行方法是在修改页面时手动调用
checkiffinished()
。这样,当没有任何更改时,就不会浪费浏览器周期来定期检查页面值,并且页面将在更改后立即更新(无需等待下一个计时器触发来检查页面)。Your code is an infinite loop if
n !== 3
because nothing you do in the while loop ever changes the value of n so thewhile
loop just runs forever waiting for the value of n to become 3. It is unclear why you put it in awhile
loop. It looks to me like it should just be anif
instead of awhile
.If you are going to use a while loop, then you have to have something that actually changes the value of n in the loop and code that ensures that you will reach the condition in your while loop to prevent the infinite loop.
As others have said, if you want to regularly check for the value of 3, then you need to repeatedly execute this function using setTimeout or setInterval not just loop infinitely. But, you don't want to execute it to often or you may drag down the performance of your site and other pages in the browser.
The best performing way to implement this would be just manually call
checkiffinished()
any time you've modified the page. That way, no browser cycles would be wasted regularly checking the page value when nothing has changed and the page would be updated immediately after the change (no waiting for the next timer to fire to check the page).呃,尝试制作一个按钮,看看它是否调用它并且不会崩溃,例如
Urmm, try making a button and see if it calls it and doesn't crash e.g
<input type="button" value="Click me!" onclick="checkiffinished()" />