问题
Here is my Pseudocode in my GWT app.
-Visible the loading Label -Loading text from properties file (may take long) -Invisible the loading Label & Visible the main HTMLPanel
So I want to use Scheduler.get().scheduleDeferred
to achive that, here is the code:
loadingLabel.setVisible(true);
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
loadingText();
}
}
loadingLabel.setVisible(false);
mainHTMLPanel.setVisible(true);
But it doesn't work correctly as it did not show the loadingLabel but show the mainHTMLPanel immediately & when i click a textbox inside the mainHTMLPanel since the Gui got frozen cos it is loading text. Then I have to wait for a while to be able to click the textbox inside mainHTMLPanel.
But if i put loadingLabel.setVisible(false);
& mainHTMLPanel.setVisible(true);
inside execute()
Then it works.
But i am not sure that is the right way to do.
So, is the following code the right way to use Scheduler.get().scheduleDeferred
?
loadingLabel.setVisible(true);
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
@Override
public void execute() {
loadingText();
loadingLabel.setVisible(false);
mainHTMLPanel.setVisible(true);
}
}
回答1:
Use a scheduler with "fixed delay" that will check each 100 milliseconds that the text content is loaded. Once the text is loaded hide your "loadingLabel" and display the "mainHTMLPanel".
mainHTMLPanel.setVisible(false);
loadingLabel.setVisible(true);
Scheduler.get().scheduleFixedDelay(new Scheduler.RepeatingCommand() {
@Override
public boolean execute() {
if (isTextLoaded()) {
loadingLabel.setVisible(false);
mainHTMLPanel.setVisible(true);
return false;
}
else {return true;}
}
}, 100);
private boolean isTextLoaded() {
// Dummy check
MyWidgetContainingText.getContent().isNotEmpty();
}
来源:https://stackoverflow.com/questions/19647905/how-to-use-scheduler-get-scheduledeferred-the-right-way-in-gwt