作者TonyQ (骨头)
看板Ajax
标题[js] javascript sleep
时间Thu Sep 20 03:37:07 2007
嗯 我要说的是sleep (ms)
让执行绪休息并锁定一阵子的code (同步模式)
一般来讲常见都是写while(true)吧
或者是弄成window.setTimeout()
while(true)真的是不用考虑了,
稳操爆browser,除非你要停的时间很短。随便跑都占掉80% cpu起跳...
可是 setTimeout() 不是同步的,
所以要用比较复杂的方式达到同步锁定的,
我在这点上脑袋打结了两天,还没有想到一个比较好的方式。:P
刚刚看到一个贱招 , 据说是IE only
(我只是看说明这样写, 没用过model对话盒...XD)
不过有效的能有效的同步锁住资料,也是利用setTimeout()
算是蛮有效解决我的问题,post过来分享看看,
并询问一下各位同好有没有更好的作法。:P
原码我看到的地方是
http://www.ozzu.com/ftopic66049.html
────────────────────────────────
/**
*@description pause( iMilliseconds ) Cause the single Javascript thread to
hald/pause/sleep/wait for a specified period of time, by opening in
modalDialog window (IE only) that modally locks the browser until it returns.
This modal dialog is not opened to any page, but uses the Javascript:
protocol to execute a javascript setTimeout. In this modal context the
setTimeout, has the desired affect of preventing any other script execution.
The sole purpose of the timeout execution script is to close the modal
dialog which will return control/unluck the browser. The intention was to
find a way to allow the UI to be updated and rendered in the middle of
function/method without the need to split the method up, remove nested calls,
or use closures. Used in this fashion to update the UI, a 0 (zero) is
usually passed (or optionally omitted altogether) so that the only delay is
for the UI to render.
*@version Note Please be aware that the user interface WILL update its
rendering (if you've made and DOM/CSS/Text changes they will appear) and this
may significantly slow down program execution if looping.
*@keywords pause sleep wait halt javascript show modal dialog set timeout
multi-threaded single thread
*@version 1.2
* @param {Object} iMilliseconds [optional] the number of milliseconds the
code will pause before returning - If no value is passed the code will
returned immediately (as if a 0 were passed)
* @return undefined there is no return value from this function
*/
function pause( iMilliseconds )
{
var sDialogScript =
'window.setTimeout( function () { window.close(); }, '
+ iMilliseconds + ');';
window.showModalDialog(
'javascript:document.writeln ("<script>'
+ sDialogScript + '<' + '/script>")');
}
--
▄▅▆▇███▇▆▅▄▃ ╰┼╯─╮ ╮
◥███████████◣ ╰┼╯=│=│
◥██████───────◣ *. ╯ ╯ ╯ の 物 语 .*
◥███████──────◣ ~ ◢◣ ◢◣
◥██████───────◤ ◥◤* 空白的世界.翼
*◥◤
◥██▁▂▃▄▅▆▇███▆▅▄▃▂▂
~telnet://tony1223.no-ip.info
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 125.232.16.81
※ 编辑: TonyQ 来自: 125.232.16.81 (09/20 03:37)
1F:→ TonyQ:好吧 , 它并不是那麽的好用 , 当它开启的时候会挡到原本页面 09/20 04:44
2F:→ TonyQ:的事件 , 真是遗憾 orz 09/20 04:45
3F:推 gpmm:能否说一下要做到什麽样的同步锁定?我之前也有简易锁锁锁过 09/20 09:24
4F:推 TonyQ:然後我今天又发现这个东西 需要开启快显示窗 09/20 15:23