0
public class TimerExample implements EntryPoint, ClickHandler {
public void onModuleLoad() {
Button b = new Button("Click and wait 5 seconds");
b.addClickHandler(this);
RootPanel.get().add(b);
}
public void onClick(ClickEvent event) {
// Create a new timer that calls Window.alert().
Timer t = new Timer() {
@Override
public void run() {
Window.alert("Nifty, eh?");
}
};
// Schedule the timer to run once in 5 seconds.
t.schedule(5000);
}
}
怎麼來的計時器仍然是方法onClick
退出後身邊?自動局部變量不應該被垃圾收集?
這是否與我們正在談論的HTML定時器的事實有關,因此該對象是否存在於自動局部變量之外?
對。計時器將由一個調度程序線程處理,該線程持有對「Timer」的引用,從而*可以*防止垃圾收集。 – oldrinb
@veer好詳細說明,我已經把它放在底部,如果這對你很好。 – edwardsmatt
沒問題! :-) – oldrinb