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.