0
votes

I have 2 timers for my GWT application. Both are repeating timers. I start running Timer1 and after some point, I cancel the timer inside its run() method and do scheduleReapeating for Timer2. Timer1's cancel occuts, but Timer2 doesn't not running. I tried running Timer2 directly and it did run. So there is nothing wrong with Timer2's code. Only that it is not running if I make it run from within Timer 1. Can't we not schedule timers whis way?

Timer timer1 = new Timer() {
@Override
public void run {
     //Some processing
     //...
     //..
     if(some_condition) {
         cancel();
         timer2.scheduleRepeating(100);
         }
     }
};
Timer2 timer 2 = new Timer() {
@Override
public void run() {
       //some processing
       //....
       //....
     }
}; 

}

2
How aboout starting timer2 before cancel() timer1? (ie. reverse the statements inside the if in timer1) - Ovi Faur
tried that too..didn't work! - somesh
Timers definitely work this way, there may be some other code in your application that is preventing this from running correctly. - Colin Alworth
ya Colin you're correct. There was a logic that was preventing the the condition needed for timer2 to start. My bad. Thanks everybody. - somesh

2 Answers

0
votes

I just ran this test code, and it worked as expected. It switches which timer runs, based on what radio button you clicked:

public class TestProj implements EntryPoint {

    //some timers
    Timer timer2, timer1;

    //buttons to operate timers
    RadioButton b1 = new RadioButton("group", "Timer 1"), 
                b2 = new RadioButton("group", "Timer 2");

    //output panel
    VerticalPanel output = new VerticalPanel();

    public void onModuleLoad() {

        RootPanel.get().add(b1);
        RootPanel.get().add(b2);
        RootPanel.get().add(output);

        timer1 = new Timer() {
            @Override
            public void run() {
                output.add(new Label("Timer 1 ran"));

                if (b2.getValue()) {
                    cancel();
                    timer2.scheduleRepeating(1000);
                }
            }
        };

        timer2 = new Timer() {
            @Override
            public void run() {
                output.add(new Label("Timer 2 ran"));

                if (b1.getValue()) {
                    cancel();
                    timer1.scheduleRepeating(1000);
                }
            }
        };

        //initial start of timer1
        timer1.scheduleRepeating(1000);
    }
}
0
votes

The code you've entered ain't the correct code and though the help can be hard to give. But the way you did put the code, it should work as a charm.

  • Is there a problem with the references to timer 1 and 2?
  • Have you subclassed the Timer?
  • Do you have any external libraries that can inflict on the timer objects in the JS world?