使用setTimeout绕过IE的长时间运行脚本警告
我以前问过这个问题,并在网上找到了一些关于这个主题的文章,但我一生都无法弄清楚这一点。我有一组计算模型的 Javascript 函数,但是存在大量循环,导致脚本需要一段时间(约 4 秒)。我不介意处理时间,但 IE 会提示警告,因为执行次数太多。
我尝试过优化我的代码,但我根本无法减少执行次数以绕过 IE 警告。因此,我认为我必须使用setTimeout函数来重置IE中的计数器。
代码很长,我只是不知道如何在 IE 中正确实现 setTimeout。我尝试模仿发现的代码 此处,但由于某种原因,它最终在第 100 次迭代后崩溃。
我不知道这是否常见,但是如果我将代码发送给他们,有人介意看一下吗?我只是不想把它贴在这里,因为它太长了。
谢谢!
编辑:按照一些人的建议,我已将代码放在 JSfiddle 上。您可以在此处找到它。感谢您的建议,如有任何问题请告诉我!
I've asked about this before, and have found a few articles online regarding this subject, but for the life of me I cannot figure this out. I have a set of Javascript functions that calculate a model, but there's a ton of looping going on that makes the script take a while (~4 seconds). I don't mind the processing time, but IE prompts with a warning since there are so many executions.
I've tried optimizing my code, but I simply can't cut down the number of executions enough to bypass the IE warning. Therefore, I figure that I must use the setTimeout function to reset the counter in IE.
The code is quite long, and I just can't figure out how to properly implement setTimeout in IE. I've tried mimicking the code found here, but for some reason it ends up crashing after the 100th iteration.
I don't know if this is common at all, but would anyone mind taking a look at the code if I sent it to them? I just wouldn't want to post it on here because it's quite lengthy.
Thanks!
EDIT: I've placed my code on JSfiddle as some people have suggested. You can find it here. Thanks for the suggestion and let me know if there are any questions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我使用的基本方法是将工作分成批次。每个批次都在一个“时期”内完成,并且在批次完成时,它调用 setTimeout() 来开始下一个时期。
假设您要运行 1000 次迭代;您可以将其分成 100 个批次。
The basic approach I use is to segment the work into batches. Each batch is done in one "epoch" and at the completion of the batch, it calls setTimeout() to kick off the next epoch.
Suppose you have 1000 iterations to run; you can segment it into batches of 100.