模态对话框导致setTimeout无效的解决方案(二)
来源:互联网 发布:淘宝日系女装店铺推荐 编辑:程序博客网 时间:2024/05/12 21:07
之前的方案一,请参照:http://rainbow702.iteye.com/admin/blogs/2040051
这篇写下方案二。
这个方案用到了HTML5中新的API,web worker。
web worker 能够产生一个独立于主线程的子线程,它们之间除了通信之外,是不会互相干扰的。而恰好,在web worker 也能够使用 setTimeout 来定时,所以,我就自然而然的想到了,把之前在主画面启动的定时器依赖web worker去做。下面就是这个方案的基本实现。
① 新建主画面,代码如下,其中,worker部分的代码是关键:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>模态对话框导致setTimeout无效的解决方案(二)</title><script> var worker = null; function start() { (event.target || event.srcElement)["disabled"] = true; // 使用web worker来进行定时if(!worker) {worker = new Worker("timer.js");worker.onmessage = function() {document.querySelector("#txt").innerText = Math.random();worker.postMessage(1000);};}worker.postMessage(1000); } function dlgOpenButton() { document.querySelector("#txt2").innerText = "模态画面打开中。。。"; var val = window.showModalDialog("dialog.html",Math.random());// 对alert框也仍然有效 // var val = window.alert("我是alert框"); document.querySelector("#txt2").innerText = "模态画面关闭了,返回值为: " + val; }</script></head><body> <button type="button" onclick="start()">启动定时器</button> <button type="button" onclick="dlgOpenButton()">打开模态对话框</button> <div> <p style="float: left;">定时更新内容:</p> <p id="txt" style="float: left;"></p> </div> <div style="float: left; clear: left;"> <p style="float: left;">模态对话框状态:</p> <p id="txt2"></p> </div></body></html>
② 在①中通过 timer.js 来创建了一个worker,下面即为 timer.js 的代码:
this.onmessage = function(task){var interval = task.data;this.setTimeout(function(){// 只需要给主线程一个信号就可以了,不需要任何返回值this.postMessage("");}, interval);};
③ 模态对话框的内容:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>我是模态对话框</title><script> window.onload = function() { var para = window.dialogArguments; document.querySelector("#fromParent").innerText = para; }; window.onunload = function() { window.returnValue = Math.random(); };</script></head><body> <div> <p>我是模态对话框,父画面传给我的值是: </p> <p id="fromParent"></p> </div> <br/></body></html>
④ 下面附上worker的工作过程
PS:
① 上述方案,不仅对showModalDialog有效,对alert之类的对话框也是有效的。
② 如果在woker中的任何地方(比如,在主画面的 onmessage 中),弹出了alert的话,上述方案将会失效,因为worker也会被alert阻断的。
0 0
- 模态对话框导致setTimeout无效的解决方案(二)
- 模态对话框导致setTimeout失效的解决方案(一)
- setTimeout无效的解决方案(三)
- Xcode更新或者重新安装导致插件无效问题的解决方案
- Mac使用zsh导致maven命令无效的解决方案
- Mac使用zsh导致maven命令无效的解决方案
- spark 数据倾斜解决方案 (二)聚合源数据以及过滤导致倾斜的key
- FormAuthenticationFilter无效的解决方案
- @Transactional 无效的解决方案
- @Transactional 无效的解决方案
- MFC中在基于对话框的窗体中使用CFileDialog导致菜单栏变灰的解决方案
- 模态对话框中输出文件流的完美解决方案
- webuploader在bootstrap模态对话框中选择文件按钮无效的问题
- JS方法setTimeout('函数名()',1000)无效的替代方法
- OSG在事件处理中由于模态对话框导致后续事件出错的问题
- onscroll事件导致flash叠影效果--setTimeout的妙用
- 错误(二)低级的finish()无效
- 【前端】浏览器不支持模态对话框解决方案
- 协方差的意义和计算公式
- Centos7 安装 yum nginx 1.8
- Callable ,Runnable 和 Thread
- 一亩三分地准备开工
- QT错误处理汇总
- 模态对话框导致setTimeout无效的解决方案(二)
- SAX解析入库sss
- repo init 出现504, 401 错误
- 登陆超时处理
- java实现获取当前日期所在的四个周的周日的功能,其中当前日期的周日是第三个,第四个是下周周日
- MyEclipse如何修改作者名称
- 2016
- Oracle 11G在用EXP 导出时,空表不能导出解决 _http://wanwentao.blog.51cto.com/2406488/545154/
- 6_3 编写函数reverse_string. 实现字符串翻转