简体   繁体   English

JavaScript的最长执行时间

[英]Maximum execution time for JavaScript

I know both ie and firefox have limits for javascript execution ( Source 1 , Source 2 ). 我知道ie和firefox都有javascript执行限制( Source 1Source 2 )。 Based on number of statements executed, I heard it was 5 million somewhere in IE and based on number of seconds in firefox: it's 10 seconds by default for my version. 根据执行的语句数量,我听说它在IE中的某个地方有500万,基于firefox中的秒数:默认情况下我的版本是10秒。

The thing I don't get is what cases will go over these limits: 我没有得到的是案例将超过这些限制:

  1. I'm sure a giant loop will go over the limit for execution time 我确信一个巨大的循环将超过执行时间的限制

  2. But will an event hander go over the limit, if itself it's execution time is under the limit but if it occurs multiple times? 但是一个事件处理者会超过限制,如果它本身的执行时间低于限制但是如果它发生多次?

Example: 例:

Lets say I have a timer on my page, that executes some javascript every 20 seconds. 假设我的页面上有一个计时器,每20秒执行一次javascript。 The execution time for the timer handler is 1 second. 计时器处理程序的执行时间为1秒。 Does firefox and ie treat each call of the timer function seperatly, so it never goes over the limit, or is it that firefox/ie adds up the time of each call so after the handler finishes, so after 200 seconds on my site (with the timer called 10 times) an error occurs even though the timer handler itself is only 1 second long? firefox和ie分别处理定时器函数的每个调用,所以它永远不会超过限制,或者是firefox /即加起来每个调用的时间所以在处理程序完成后,所以在我的网站上200秒后(与定时器调用10次)即使定时器处理程序本身只有1秒长,也会发生错误?

The following article by Nicholas C. Zakas discusses how and when different browsers interrupt long running JavaScript code: Nicholas C. Zakas的以下文章讨论了不同浏览器如何以及何时中断长时间运行的JavaScript代码:

Breaking long processing code into small chunks and launching them with timers is in fact one way to get around this problem. 将长处理代码分解成小块并用定时器启动它实际上是解决这个问题的一种方法。 The following Stack Overflow post suggests a method to tackle this: 以下Stack Overflow帖子提出了解决此问题的方法:

On the other hand, web workers would be more suited for long running processing, since their execution happens in a separate process, and therefore does not block the UI thread: 另一方面, Web worker更适合长时间运行的处理,因为它们的执行发生在一个单独的进程中,因此不会阻止UI线程:

However web workers are not supported in Internet Explorer yet, and they would not have access to the DOM. 但是,Internet Explorer中不支持Web工作者,并且他们无法访问DOM。

The event handler is considered a new execution context - the time limit is reset. 事件处理程序被视为新的执行上下文 - 重置时间限制。

If you need to do even more computation, take a look at WebWorkers. 如果您需要进行更多计算,请查看WebWorkers。

I too stuck in this kind of JS error. 我也陷入了这种JS错误。 I also get the solution, unfortunately only for IE, which tells to edit the registry (regedit command) and update the number of JS commands to allow. 我也得到了解决方案,不幸的是只有IE,它告诉编辑注册表(regedit命令)并更新允许的JS命令的数量。 Read more here http://support.microsoft.com/?kbid=175500 . 在此处阅读更多信息http://support.microsoft.com/?kbid=175500

But this doesn't seems to be a good solution to me bcoz if you are developing a web application you can not ask every user to use only IE and update his system registry. 但是,如果你正在开发一个web应用程序,你不能要求每个用户只使用IE并更新他的系统注册表,这对我来说似乎不是一个好的解决方案。

As a workaround: If your data to process at client end is really large then you can put some manual delays between fix set of events processing. 解决方法:如果您在客户端处理的数据非常大,那么您可以在修复事件集处理之间设置一些手动延迟。 For ex. 对于前者 If there are 100 events of any task you want to process then you can break them in to 10 sets of 100 and process each of them in say 100ms. 如果您要处理任何任务的事件有100个,那么您可以将它们分成10组100个并按100毫秒处理它们中的每一个。 To know how to do that follow below link: 要知道如何操作,请按照以下链接:

http://www.nczonline.net/blog/2009/01/13/speed-up-your-javascript-part-1/ http://www.nczonline.net/blog/2009/01/13/speed-up-your-javascript-part-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM