elisp 中有没有像 JavaScript 的 setTimeout 函数

自己查到有 sleep-for sit-for run-with-idle-timer 但与 setTimeout 有些区别

想有 与 setTimeout 一样 不阻塞当前线程,且能被可 ClearTimeout 的功能

不会 js,所以根本不知道你在问什么

org的番茄钟实现可以参照参照

consult 的源码使用了 timer来优化性能,这个代码看起来也比较简单。

若是在CL,那就多线程+sleep咯? 咱之前写了

子龙山人的是正解,可以用 run-with-timer 设置 、 cancel-timer 取消

一个简单的小例子:

(defvar simple-timer (run-with-timer 0 3 #'message "hello, world"))
(cancel-timer simple-timer)

定义一个变量 simple-timer 用来保存 run-with-timer 的返回值, run-with-timer 的参数依次是,计时器到点时延迟执行的时间、计时时间,执行函数,剩余参数。

那么上面的例子就是每隔 3 秒立刻(延迟为0)执行 (message “hello, world”)

下面是详细的文档:

(run-with-timer SECS REPEAT FUNCTION &rest ARGS)

Perform an action after a delay of SECS seconds.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
SECS and REPEAT may be integers or floating point numbers.
The action is to call FUNCTION with arguments ARGS.

This function returns a timer object which you can use in ‘cancel-timer’.
(cancel-timer TIMER)

Remove TIMER from the list of active timers.
1 个赞