更新时间:2022-12-02 18:20:25
您无法像 Window.confirm()
一样使用它。问题是,网页中的所有JavaScript运行在单个线程中。您会注意到,只要打开标准确认对话框,页面的其余部分就会失效。这是因为一个JavaScript线程被阻塞,等待 confirm()
返回。如果您要为对话创建一个类似的方法,只要它正在等待该方法返回,则不会处理用户生成的事件,因此您的对话将不起作用。我希望这是有道理的。
You're not going to be able to have it work in exactly the same way as Window.confirm()
. The problem is that all of the javascript in a web page runs in a single thread. You'll notice that as long as a standard confirm dialog is open, the rest of the page goes dead. That's because the one javascript thread is blocked, waiting for confirm()
to return. If you were to create a similar method for your dialog, as long as it was waiting for that method to return no user generated events would be processed and so your dialog wouldn't work. I hope that makes sense.
您能够做的***的事情与GWT库为RPC调用做的类似 - AsyncCallback
界面。您甚至可以自己重新使用该接口,或者您可能更愿意推出自己的接口:
The best you will be able to do is similar to what the GWT library does for RPC calls -- the AsyncCallback
interface. You could even reuse that interface yourself, or you might prefer to roll your own:
public interface DialogCallback {
void onOk();
void onCancel();
}
而不是 Window.confirm(String) code>,你的方法签名会更像
Dialog.confirm(String,DialogCallback)
。然后你的对话框只保留一个对你传入的回调的引用,以及你调用 onOk $ c>时在你的代码中执行
/ code>和
onCancel
。
Instead of Window.confirm(String)
, your method signature will be more like Dialog.confirm(String,DialogCallback)
. Then your dialog just keeps a reference to the callback that's passed in, and where you have // do something
in your code you make calls to onOk
and onCancel
.