iframe是否与所有者在同一个线程上运行? [英] Does iframe runs on the same thread as the owner?

查看:1558
本文介绍了iframe是否与所有者在同一个线程上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项CPU密集型工作要做,我不想降低用户体验。因为网络工作者( http://ejohn.org/blog/web-workers/ )是一个新功能,并不是所有浏览器都支持,我想打开一个带有HTML + JS的iframe,它将执行所有脏工作并使用一些跨域通信来传递结果。
不幸的是我注意到iframe的所有者遭受了iframe窗口的CPU工作。

I have a CPU intensive work to do and i don't want to degrade the user experience. since web workers (http://ejohn.org/blog/web-workers/) are a new feature and are not supported by all browsers, i thought to open an iframe with an HTML + JS that will do all the dirty work and using some cross-domain communications to pass on the results. Unfortunately i noticed that the owner of the iframe suffers from the CPU work of the iframe window.

此行为是否符合设计要求?有没有办法解决这个问题?

Does this behavior is as designed? is there a way to solve this?

推荐答案

模拟多线程的一种方法是使用Javascript函数做一点点工作,然后使用相同的函数调用 setTimeout ;然后该函数将做一些工作并再次调用 setTimeout ,此循环将永久持续或直到它们关闭帧或您发出信号停止工作。 MDN有一个很好的例子来说明如何进行设置。

One way to sort of simulate multi-threadedness would be to have a Javascript function do a little bit of work, then call setTimeout with that same function; then the function will do a little work and call setTimeout again, and this cycle will continue forever or until they close the frame or you signal to stop working. MDN has a good example of how to set this up.

在超时之间,Javascript不应占用任何处理器时间。你可能需要玩一下才能看出你的超时时间应该是多长时间 - 1ms可能太短了,但1s绝对太长了。另一个因素是运行该作业的计算机的处理器速度,因此您可能需要通过Javascript在客户端进行一些伪基准测试,然后才能确定每次延迟的时间。

Between timeouts, Javascript should not consume any processor time. You might have to play around a little bit to see how long your timeouts should be -- 1ms is probably way too short, but 1s is definitely way too long. Another factor will be the processor speed of the computer running the job, so you might need to do some pseudo-benchmarking on the client's side via Javascript before you can determine how long to delay each time.

这篇关于iframe是否与所有者在同一个线程上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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