GWT PopupPanel未在正确的时间显示

vla*_*918 2 javascript gwt

我有一个单击按钮就调用的函数,它需要很长时间才能创建一些元素并将其附加到DOM,所以我想在函数完成操作后显示带有“正在加载”图标的PopupPanel。做。请注意,我的函数不会调用服务器,它只会在UI中创建一些元素,这些元素需要很长时间才能计算出来,因此我没有onSuccess()事件或回调函数。

所以我有我的PopupPanel:

pp = new PopupPanel();
pp.setTitle("loading....");
pp.setGlassEnabled(true);
Run Code Online (Sandbox Code Playgroud)

然后在点击处理程序上,我有以下代码:

public void onClick(ClickEvent event) {
  pp.show();
  functionWhichTakesaLongTimeToExecute();               
  pp.hide();
}
Run Code Online (Sandbox Code Playgroud)

由于JavaScript是单线程的,因此我期望PopUp会出现,然后执行functionWhichTakesaLongTimeToExecute(),然后将PopUp隐藏,但是发生的是首先执行functionWhichTakesaLongTimeToExecute(),这需要很长时间才能将元素附加到DOM,并且只有在完成工作后才显示PopUp,然后将其隐藏。好像代码是:

functionWhichTakesaLongTimeToExecute(); 
pp.show();                  
pp.hide();
Run Code Online (Sandbox Code Playgroud)

更让我困扰的是,如果我在pp.show()之后添加Window.alert(“ test”),这将中断流程,直到按下警报中的“确定”按钮,这会导致PopUp出现在警报和在调用functionWhichTakesaLongTimeToExecute()之前。因此,以下代码按预期工作:

pp.show();
Window.alert("test");
functionWhichTakesaLongTimeToExecute();                 
pp.hide(); 
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么在调用functionWhichTakesaLongTimeToExecute()之前为什么不显示PopUp,而是“等待” functionWhichTakesaLongTimeToExecute()执行,直到它显示之后,以及为什么添加Window.alert(“ test ”)使其正确显示?
PS:我已经在IE8和Chrome中测试了代码,并且行为相同。

And*_*gin 5

这是您可以执行的操作:

pp.show();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {

    @Override
    public void execute() {
        functionWhichTakesaLongTimeToExecute();
        pp.hide();
    }
});
Run Code Online (Sandbox Code Playgroud)

我在这里解释:GWT:计时器和调度程序类