how to cancel a timer's existing schedule

user301 picture user301 · Oct 27, 2011 · Viewed 10.4k times · Source

Currently I am using a timer to execute some function every interval time. However, later on when I want to change the interval of executing the function, I cannot cancel the previous schedule. how this can be solved? Thanks

Answer

Mats Hofman picture Mats Hofman · Oct 27, 2011

With the timer.cancel() method you can cancels the Timer and all scheduled tasks. (see API documentation) or you can call the cancel method on your TimerTask timertask.cancel() (see API documentation)

If you want to change the scheduled time you should cancel the TimerTask and add a new TimerTask.