with-timeout

with-timeout is a Lisp macro in `timer.el'.

(with-timeout (SECONDS TIMEOUT-FORMS...) BODY)

Run BODY, but if it doesn't finish in SECONDS seconds, give up.
If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
The timeout is checked whenever Emacs waits for some kind of external
event (such as keyboard input, input from subprocesses, or a certain time);
if the program loops without waiting in any way, the timeout will not
be detected.