0

I'm working on creating a timer in Java, and was wondering how I can use timer.cancel to then create a new timer which has a different interval.

My code looks something like this:

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        gameView.invalidate();
                      //  timer.cancel(); - here, need to somehow restart timer with new interval
                    }
                });
            }
        }, 0, TimerInterval.interval);

In another view, I'm modifying TimerInterval.interval, but this doesn't do anything/update the timer, because I need to somehow completely cancel the timer and create a new one, but I'm not sure how to do this.

Any help with this matter would be appreciated.

FZs
  • 16,581
  • 13
  • 41
  • 50
Evan
  • 1,892
  • 2
  • 19
  • 40
  • cancel the current timer / task and schedule a new one – luk2302 Aug 22 '20 at 17:58
  • I understand that's the basic idea, but I'm not sure the syntax for this without creating some recursive loop - I've looked online but couldn't find anything applicable for this case. If you have a resource with this you could point me to, it'd be much appreciated. – Evan Aug 22 '20 at 18:04
  • This [SO Post](https://stackoverflow.com/a/10336018/4725875) **may** help you. – DevilsHnd - 退職した Aug 22 '20 at 21:36
  • haha yeah i was looking at that one earlier, but I'll check it out again, thanks – Evan Aug 22 '20 at 21:39

0 Answers0