暂停JavaScript执行而不锁定浏览器 [英] Halt JavaScript execution without locking up the browser

查看:133
本文介绍了暂停JavaScript执行而不锁定浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您是否能够在不锁定浏览器的情况下暂停JavaScript执行?通常停止执行的方式是执行无限 while() -loop,但是对于FireFox,它会锁定浏览器直到循环结束。 / p>

您对此有何看法?






我试图覆盖 window.confirm()使用HTML实现我自己的对话框。我这样做,所以我不必更改现有代码(这是一个相当大的代码库)。



我需要能够暂停执行以允许用户 - 输入;反过来返回一个像标准确认函数那样的布尔值:

  if(confirm(...))
{
//用户按下确定
}
其他
{
//用户按下取消
}





更新



据我所知;这不能使用 setTimeout() setInterval()来完成,因为这些函数会异步执行给它们的代码。

解决方案

confirm() prompt() alert()是特殊功能 - 它们将JavaScript沙箱调用到浏览器中,浏览器暂停JavaScript执行。你不能做同样的事情,因为你需要将你的功能构建成JavaScript。



我不认为有一个很好的方法可以放弃一个替代品而不做一些重组如下:

  myconfirmfunction(function(){
/ * OK callback * /
},function(){
/ *取消回调* /
});


Are you able to halt JavaScript execution without locking up the browser? The way you would normally halt execution is to do an infinite while()-loop, but in the case of FireFox, it locks up the browser until the loop has ended.

What's your take on this?


I am trying to override window.confirm() to implement my own dialog using HTML. I am doing this so I don't have to change existing code (it's a pretty big code-base).

I need to be able to halt execution to allow user-input; to in turn return a boolean like the standard confirm function does:

if (confirm("..."))
{
    // user pressed "OK"
}
else
{
    // user pressed "Cancel"
}



Update

To my knowledge; this cannot be done using setTimeout() or setInterval() since these functions execute the code thats given to them asynchronously.

解决方案

confirm() prompt() and alert() are special functions--they call out of the JavaScript sandbox into the browser, and the browser suspends JavaScript execution. You can't do the same thing, since you need to build your functionality into JavaScript.

I don't think there's a great way to drop in a replacement without doing some restructuring along the lines of:

myconfirmfunction(function() {
   /* OK callback */
}, function() {
   /* cancel callback */
});

这篇关于暂停JavaScript执行而不锁定浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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