从大型JavaScript操作减少客户端的明显滞后的方法 [英] Methods to reduce apparent lag to client from large javascript operations

查看:74
本文介绍了从大型JavaScript操作减少客户端的明显滞后的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的Chrome扩展程序的一部分,我正在对文档结束事件中的很多(多达3000个)元素执行一些正则表达式替换。在最糟糕的情况下,在Chrome 34.0.1847.116米相当漂亮的PC上,操作可能需要180秒完成,在此期间,网页被冻结。



<是否有,如果是这样的话,最好的办法是减少较长时间的操作,或给操作一个较低优先级,以便它不会占用100%运行。



脚本非常嵌套jQuery每个函数都使用大表达式运行正则表达式替换。这些表达式搜索3000-4000个单词,然后用一些html替换它。一个例子是(搜索特定的reddit名字)

  /(\s | / u / | ^)(name1 | name2 | name3 | ... | name500)([^ \w] | $)/ 

如果有帮助,您可以在这里看到完整的源代码



感谢您的时间。

解决方案

您可以使用 setTimeout 函数和完成时的回调来在新线程中运行长操作。



请参阅 Javascript创建新的线程


As part of my Chrome Extension, I am performing a few regex replaces on a lot of (upwards of 3000) elements on the document end event. In the worst cases, in Chrome 34.0.1847.116 m on a pretty decent PC, the operation can take >180 seconds to complete, and during this time, the webpage is frozen.

Is there, and if so, what is the best way to either mitigate the operation over a longer time span, or give the operation a "lower priority" so that it doesn't take 100% while running.

The script is pretty much nested jQuery each functions all running a regex replace using a large expression. These expressiond search for 3000-4000 words and then replaces it with some html. An example one is (searching for specific reddit names)

/(\s|/u/|^)(name1|name2|name3|...|name500)([^\w]|$)/

If it helps, you can see the full source here.

Thank you for your time.

解决方案

You can run the long operations in a new thread using the setTimeout function and a callback on completion.

See Javascript Create New "Thread"

这篇关于从大型JavaScript操作减少客户端的明显滞后的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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