How to remove a function from run-with-idle-timer?

I added the lambda() function to run-with-idle-timer as follows:

 (run-with-idle-timer my-configurable-idle-time t (lambda () ;;; do something ) ) 

Can this function be removed again at a later point from the idle timer trigger?

+6
source share
3 answers

Yes, run-with-idle-timer returns a timer object that you can pass to cancel-timer .

If you did not save the timer object, you can manually change the timer-idle-list .

See also Listing Emacs Timers .

+12
source

I also came across a similar situation where I wanted to kill the timer I started with:

 (setq my-timer (run-with-timer 5 5 'my-func)) 

but

 (cancel-timer my-timer) 

did not work because he said that my timer was not set (I do not know why this happened).

In addition to the method of the first poster, it can be killed with:

 (cancel-function-timers 'my-func) 

This cancels all timers calling the 'my-func function.

To kill him by changing the timer list, which I also checked, I did the following:

 (length timer-list) ;; I had two timers..one good, one bad (cdr timer-list) ;; I verified the last was the one I wanted to keep (setq timer-list (cdr timer-list)) ;; I reset timer-list 

Obviously, this list structure will be different, so you will have to adjust it accordingly. Replace "timer-idle-list" if you started your timer with (run-with-idle-timer)

This should also work if you started your timer with "gamegrid-start-timer" and "gamegrid-kill-timer" does not work, since "gamegrid-start-timer" is essentially just a shell for "run-with-timer "

+6
source

Another solution: if you are out of luck and you cannot cancel the timer, you can always use

 (defvar my-timer-enabler t) (run-with-idle-timer my-configurable-idle-time t (lambda () (when my-timer-enabler ;;; do something ))) 

That way you can disable the timer by setting my-timer-enabler to nil . And you can turn on the timer later by simply returning var to t .

+1
source

All Articles