Function: run-at-time
run-at-time is an interactive and byte-compiled function defined in
timer.el.gz.
Signature
(run-at-time TIME REPEAT FUNCTION &rest ARGS)
Documentation
Perform an action at time TIME.
Repeat the action every REPEAT seconds, if REPEAT is non-nil. REPEAT may be a non-negative integer or floating point number. TIME should be one of:
- a string giving today's time like "11:23pm"
(the acceptable formats are HHMM, H:MM, HH:MM, HHam, HHAM,
HHpm, HHPM, HH:MMam, HH:MMAM, HH:MMpm, or HH:MMPM;
a period . can be used instead of a colon : to separate
the hour and minute parts);
- a string giving a relative time like "90" or "2 hours 35 minutes"
(the acceptable forms are a number of seconds without units
or some combination of values using units in timer-duration-words);
- nil, meaning now;
- a number of seconds from now;
- a value from encode-time;
- or t (with non-nil REPEAT) meaning the next integral multiple
of REPEAT. This is handy when you want the function to run at
a certain "round" number. For instance, (run-at-time t 60 ...)
will run at 11:04:00, 11:05:00, etc.
The action is to call FUNCTION with arguments ARGS.
This function returns a timer object which you can use in
cancel-timer.
Probably introduced at or before Emacs version 19.26.
Key Bindings
Aliases
type-break-run-at-time (obsolete since 30.1)
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/timer.el.gz
(defun run-at-time (time repeat function &rest args)
"Perform an action at time TIME.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
REPEAT may be a non-negative integer or floating point number.
TIME should be one of:
- a string giving today's time like \"11:23pm\"
(the acceptable formats are HHMM, H:MM, HH:MM, HHam, HHAM,
HHpm, HHPM, HH:MMam, HH:MMAM, HH:MMpm, or HH:MMPM;
a period `.' can be used instead of a colon `:' to separate
the hour and minute parts);
- a string giving a relative time like \"90\" or \"2 hours 35 minutes\"
(the acceptable forms are a number of seconds without units
or some combination of values using units in `timer-duration-words');
- nil, meaning now;
- a number of seconds from now;
- a value from `encode-time';
- or t (with non-nil REPEAT) meaning the next integral multiple
of REPEAT. This is handy when you want the function to run at
a certain \"round\" number. For instance, (run-at-time t 60 ...)
will run at 11:04:00, 11:05:00, etc.
The action is to call FUNCTION with arguments ARGS.
This function returns a timer object which you can use in
`cancel-timer'."
(interactive "sRun at time: \nNRepeat interval: \naFunction: ")
(unless (or (null repeat)
(and (numberp repeat)
(>= repeat 0)))
(error "Invalid repetition interval: %s" repeat))
(let ((timer (timer-create)))
;; Special case: nil means "now" and is useful when repeating.
(unless time
(setq time (current-time)))
;; Special case: t means the next integral multiple of REPEAT.
(when (and (eq time t) repeat)
(setq time (timer-next-integral-multiple-of-time nil repeat))
(setf (timer--integral-multiple timer) t))
;; Handle numbers as relative times in seconds.
(when (numberp time)
(setq time (timer-relative-time nil time)))
;; Handle relative times like "2 hours 35 minutes".
(when (stringp time)
(when-let* ((secs (timer-duration time)))
(setq time (timer-relative-time nil secs))))
;; Handle "11:23pm" and the like. Interpret it as meaning today
;; which admittedly is rather stupid if we have passed that time
;; already. (Though only Emacs hackers hack Emacs at that time.)
(when (stringp time)
(require 'diary-lib)
(let ((hhmm (diary-entry-time time))
(now (decode-time)))
(when (>= hhmm 0)
(setq time (encode-time 0 (% hhmm 100) (/ hhmm 100)
(decoded-time-day now)
(decoded-time-month now)
(decoded-time-year now)
(decoded-time-zone now))))))
(timer-set-time timer time repeat)
(timer-set-function timer function args)
(timer-activate timer)
timer))