Discussion:
emacs run-with-timer
(too old to reply)
David Hume
2014-04-23 16:09:22 UTC
Permalink
I have this code:

(defun timeless ()
(interactive)
(run-with-timer 10 10 (message "Hello Time"))
(sit-for 120)
)

I was expecting that I would be able to type "timeless" and it would
output the message "Hello Time" every 10 seconds. But it seems to output
it only once. (Removing the sit-for line makes no difference).

What I am really trying to do is write something which will check for
new news periodically, and then perhaps notify me if there is any. My
attempts with run-with-idle-timer didn't seem to work either.

How is this function meant to be used? Am I using it in the wrong way?
a***@gmail.com
2019-10-01 13:24:17 UTC
Permalink
Post by David Hume
...
(defun timeless ()
(interactive)
(run-with-timer 10 10 (message "Hello Time"))
(sit-for 120)
)
I was expecting that I would be able to type "timeless" and it would
output the message "Hello Time" every 10 seconds. But it seems to output
it only once. (Removing the sit-for line makes no difference).
...
Holy cow this is an old message, and no answer! (Not any visible on Google, anyway.) This group is not so active, apparently.. Here's a for-the-record response.

Two things: 1) after all these years the function is now called "run-at-time"; 2) you need to pass it a function, and "(message ...)" does not evaluate to a function. What has happened is "(message ...)" was evaluated once while putting together the call to "run-with-timer".

Instead, you need to maybe put "(lambda () (message ...))", which evaluates to a nameless function that will in turn call "message" in its body.

(run-at-time 10 10 (lambda () (message "Hello Time)))

You could defun:

(defun my-message () (message "Hello Time"))

(run-at-time 10 10 (function my-message))

"run-at-time" can accept arguments to pass on to the scheduled function, so if you just want to (in this example) print a constant string, you specify the "message" function as the argument rather than writing a lambda:

(run-at-time 10 10 (function message) "Hello Time")

or, using the #' reader syntax:

(run-at-time 10 10 #'message "Hello Time")

Loading...