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

查看:26
本文介绍了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天全站免登陆