71843

Timer is still firing after Clicking on other Links in GWT

Question:

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!

Answer1:

<h3>Steps to follow</h3> <ul><li>use window.onunload event that is called when page is refreshed</li> <li>first export cancelTimer() method to java script using JSNI and register cancelTimerFunction as java script function that is called on page unload</li> <li>cancel timer on window close</li> </ul>

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; }-*/;

Recommend

  • Is it possible to build jquery like stand-alone widgets (like sliders or date selectors etc.) with G
  • Create a string using Jinja2 template
  • Modifying files nested in tar archive
  • How to authenticate user name and password against Active Directory Federation Services (ADFS)?
  • Should a C# struct have only read-only properties
  • Easy way to convert a Bitmap and Png Image to text and vice versa
  • Stacked Bar Chart with percentage composition inside the Bar and total above the Bar in JFreeChart
  • javascript variables, What does var x = a = {} do?
  • GWT Widget.addHandler
  • Get the last date of each month in a list of dates in Python
  • How secure are apple APNS push notifications?
  • How to define an array of floats in Shader properties?
  • WARNING:root:Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER. Wit
  • Loading fixtures in sails tests
  • Receive mouse move even cursor is outside control
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • what does prefix @- mean in makefile?
  • How to render a pixel array most efficiently to a window in c++?
  • npm 5.4.1 install/uninstall all failing
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Assign variable to the value in HTML
  • How to use carriage return with multiple line?
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Perl system calls when running as another user using sudo
  • sending/ receiving email in Java
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?