Ignore the below; the best way to accomplish this seems to be to add a function to auto-save-hook, which runs on both an idle timer and a keystroke counter.
In other words,
(defun run-every-once-in-a-while (fun) "Run a command every once in a while, if possible when emacs is idle." (add-hook 'auto-save-hook fun))
I plan to implement a command, possibly called run-when-idle or run-every-once-in-a-while, that can be used to run background tasks, with the following requirements:
For example, you might want to autosave some data at least every five minutes, but not more often than every minute, and you don’t want the saving to bother you if it can help it.
I haven’t found any current emacs command to do something like this.
The interface will probably be similar to that of timer.el, and the implementation of a “job” will probably be as a set of interacting timers. This might require the use of lexical variables (via lexical-let, if it works right).
How probable it is run-with-idle-timer is deferred indefinitely? It can happen, of course, as lots of other things, but is it probable judging from your everyday Emacs usage?
I think the best answer to this is to just add things to auto-save-hook. It’s what I’ve been doing.
So when you added stuff to auto-save-hook it was deferred indefinitely very often, that’s why you want to reimplement this feature in a new function?
No, I didn’t find auto-save-hook until after I started working on this. I decided that auto-save-hook is close enough to what I want and requires much less effort to use, rather than come up with a completely new system. So to be clear, auto-save-hook does not implement all of the above features, but it is almost as good.