[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Timer variable binding
From: |
Johan Andersson |
Subject: |
Timer variable binding |
Date: |
Tue, 7 Jan 2014 15:53:25 +0100 |
Hi,
I have some questions regarding timers.
This code will start a timer and the first time the callback runs, the
timer is canceled. Works great!
(let ((timer (run-at-time 0 1 (lambda ()
(cancel-timer timer))))))
In the above example I can access the timer variable inside the function
callback. But in this code, I cannot access the variable my-var. Nothing is
printed. In Emacs 24.3.1 I see no error, but in 24.3.50.1 I get
(void-variable my-var). Why is there no error in 24.3.1?
(let* (timer (my-var 10))
(setq timer (run-at-time 0 1 (lambda ()
(print my-var)
(cancel-timer timer)))))
I can solve this by passing the my-var variable as argument to run-at-time,
but then that same value will be passed to the function callback each time.
Let's say for example that I want to run a timer 10 times, I can't do this
using the code below, because my-var will always have value 10:
(let* (timer (my-var 10))
(setq timer (run-at-time 0 1 (lambda (my-var)
(setq my-var (1- my-var))
(when (= my-var 0)
(cancel-timer timer)))
my-var)))
Something else worth noting is that in the callback function, it is
possible to access globally defined variables.
Can someone please explain these weird behaviors?
- Timer variable binding,
Johan Andersson <=