JavaScript执行会暂时挂起页面 [英] JavaScript execution hangs page momentarily

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

问题描述

我有一个大量使用 jQuery / JavaScript 的网络应用程序。它在内存中包含一个大型数组,用户可以通过键入文本框来过滤它。

I have a web application which uses jQuery/JavaScript heavily. It holds a large array in memory, and the user filters it by typing in a textbox.

问题:当过滤算法运行时,应用程序变得无响应,浏览器甚至可能会询问用户是否让脚本继续。

Problem: When the filtering algorithm runs, the application becomes non-responsive and the browser may even ask the user whether to let the script continue.

最理想的情况是,我希望过滤器功能在单独的线程中运行,以避免无响应。这有可能吗?或者,我想展示一个旋转沙漏或类似的东西,但浏览器似乎无法在运行繁重的脚本时显示动画GIF。

Optimally, I would like the filter function to run in a separate thread, to avoid non-responsiveness. Is this possible in any way? Alternatively, I would like to show a rotating hourglass or similar, but browsers seem unable to display animated GIFs when running heavy scripts.

攻击问题的最佳方法是什么?

What is the best way of attacking the problem?

推荐答案

浏览器在主事件处理线程中执行脚本。这意味着任何长时间运行的脚本都可以保留浏览器队列。

Browsers execute scripts in the main event processing thread. This means any long running scripts can holdup the browser queue.

您应该将过滤器逻辑拆分为块并在超时回调。您可以在执行之间使用0毫秒的间隙。 0 milli只是对浏览器的建议,但浏览器将使用后续回调之间的差距来清除其事件队列。超时通常是在浏览器环境中应该运行多长时间的脚本以防止页面挂起。

You should split your filter logic into chunks and run them on timeout callback's. You may use a gap of 0 mills between executions. 0 milli's is just a suggestion to the browser, but the browser will use the gap between subsequent callbacks to clear its event queue. Timeout's is generally how long running scripts are ought to be executed in the browser environment to prevent the page from "hanging".

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

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