在点击GWT中的其他链接之后,定时器仍在点燃 [英] Timer is still firing after Clicking on other Links in GWT

查看:121
本文介绍了在点击GWT中的其他链接之后,定时器仍在点燃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个计时器,即使我清除面板并加载了其他模型,它也会持续启动......我的问题是,如何在卸载模型时取消计时器?


$
$ b

  public Display(List< Clarification> result){

if(result.size()== 0){
Window.alert(EMPTY);
} else {


RootPanel.get(Dev1)。clear();
t = new Timer(){
public void run(){

cd = new ClarificationDispatcher();
cd.getClarificationsCount(result.size());
}
};
t.scheduleRepeating(5000);

}



我尝试取消然而,定时器onUnload()方法,我不相信它会被调用......
$ b

谢谢!

解决方案

步骤遵循




  • 使用 window.onunload code>在刷新页面时调用的事件
  • 第一次导出 cancelTimer()使用JSNI和register注册java脚本的方法 cancelTimerFunction 作为在页面上调用的Java脚本函数

  • 在窗口关闭时取消定时器
  • ul>

    代码:

      import com.google.gwt.user.client。定时器; 
    import com.google.gwt.user.client.Window;

    private static Timer timer = null;

    public void onModuleLoad(){
    exportCancelTimer();

    最终标签标签=新标签(你好);
    timer = new Timer(){

    @Override
    public void run(){
    label.setText(Hello+ Math.random()* 100 );
    }

    };

    timer.scheduleRepeating(500);
    RootPanel.get()。add(label);

    Window.addCloseHandler(new CloseHandler< Window>(){
    $ b $ @Override
    public void onClose(CloseEvent< Window> event){
    timer .cancel();
    }
    });



    $ b public static void cancelTimer(){
    if(timer!= null){
    System.out.println() 取消);
    timer.cancel();



    public static native exportCancelTimer()/ * - {
    $ wnd.cancelTimerFunction = $ entry(@ com.xyzGWTProject :: cancelTimer( ));
    $ wnd.onunload = $ wnd.cancelTimerFunction;
    } - * /;


    So I have a timer and it keeps on firing even though I cleared the Panel and loaded other model... my question is, how to cancel a timer when I unload a model?

    So here is part of my code

    public Display(List<Clarification> result) {
    
        if (result.size() == 0) {
            Window.alert("EMPTY");
        } else {
    
    
            RootPanel.get("Dev1").clear();
             t = new Timer() {
                public void run() {
    
                    cd = new ClarificationDispatcher();
                    cd.getClarificationsCount(result.size());
                }
            };
            t.scheduleRepeating(5000);
    

    }

    I tried to cancel the Timer onUnload() method however, I don't believe it is getting called at all...

    Thanks!

    解决方案

    Steps to follow

    • use window.onunload event that is called when page is refreshed
    • first export cancelTimer() method to java script using JSNI and register cancelTimerFunction as java script function that is called on page unload
    • cancel timer on window close

    Code:

    import com.google.gwt.user.client.Timer;
    import com.google.gwt.user.client.Window;
    
    private static Timer timer = null;
    
    public void onModuleLoad() {
        exportCancelTimer();
    
        final Label label = new Label("Hello ");
        timer = new Timer() {
    
            @Override
            public void run() {
                label.setText("Hello " + Math.random() * 100);
            }
    
        };
    
        timer.scheduleRepeating(500);
        RootPanel.get().add(label);
    
        Window.addCloseHandler(new CloseHandler<Window>() {
    
            @Override
            public void onClose(CloseEvent<Window> event) {
                timer.cancel();
            }
        });
    
    }
    
    
    public static void cancelTimer() {
        if (timer != null) {
            System.out.println("cancel");
            timer.cancel();
        }
    }
    
    public static native void exportCancelTimer() /*-{
        $wnd.cancelTimerFunction = $entry(@com.x.y.z.GWTProject::cancelTimer());
        $wnd.onunload = $wnd.cancelTimerFunction;
    }-*/;
    

    这篇关于在点击GWT中的其他链接之后,定时器仍在点燃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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