JavaScript的最长执行时间 [英] Maximum execution time for JavaScript

查看:184
本文介绍了JavaScript的最长执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道ie和firefox都有javascript执行限制( Source 1 来源2 )。根据执行的语句数量,我听说它在IE中的某个地方有500万,基于firefox的秒数:默认为我的版本是10秒。

I know both ie and firefox have limits for javascript execution (Source 1, Source 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.

我没有得到的是什么案例将超过这些限制:

The thing I don't get is what cases will go over these limits:


  1. 我确定一个巨大的循环将会结束执行时间的限制

  1. I'm sure a giant loop will go over the limit for execution time

但是如果一个事件处理器的执行时间超出限制但是如果它多次出现会超过限制吗? / p>

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?

示例:

让我说我有一个计时器我的页面,每20秒执行一些javascript。计时器处理程序的执行时间为1秒。 firefox和ie分别处理定时器函数的每个调用,所以它永远不会超过限制,或者是firefox /即加起来每个调用的时间所以在处理程序完成后,所以在我的网站上200秒后(与计时器调用10次)即使计时器处理程序本身只有1秒长,也会发生错误?

Lets say I have a timer on my page, that executes some javascript every 20 seconds. The execution time for the timer handler is 1 second. 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?

推荐答案

以下文章 Nicholas C. Zakas 讨论了不同浏览器如何以及何时中断长时间运行的JavaScript代码:

The following article by Nicholas C. Zakas discusses how and when different browsers interrupt long running JavaScript code:

  • What determines that a script is long-running?

将长处理代码分解成小块并使用定时器启动它实际上是解决此问题的一种方法。以下Stack Overflow帖子提出了解决此问题的方法:

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:

  • Show javascript execution progress

另一方面, 网络工作者将更适合长时间运行的处理,因为他们的执行发生在一个单独的进程中,因此不会阻止UI线程:

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:

  • Mozilla Dev Center: Using web workers
  • John Resig: Computing with JavaScript Web Workers
  • Nicholas C. Zakas: Experimenting with web workers

但是,Internet Explorer中不支持Web工作者,他们也无法访问DOM。

However web workers are not supported in Internet Explorer yet, and they would not have access to the DOM.

这篇关于JavaScript的最长执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆