Function: timeout-debounced-func

timeout-debounced-func is an autoloaded and byte-compiled function defined in timeout.el.gz.

Signature

(timeout-debounced-func FUNC &optional DELAY DEFAULT)

Documentation

Return a debounced version of function FUNC.

The debounced function runs DELAY seconds after it is called. DELAY defaults to 0.5 seconds. DELAY can be a number, a symbol (whose value is a number), or a function (that evaluates to a number). When passed a symbol or function, it is evaluated at runtime for dynamic duration.

The function returns immediately with value DEFAULT when called the first time. On future invocations, the result from the previous call is returned.

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/timeout.el.gz
;;;###autoload
(defun timeout-debounced-func (func &optional delay default)
  "Return a debounced version of function FUNC.

The debounced function runs DELAY seconds after it is called.  DELAY
defaults to 0.5 seconds.  DELAY can be a number, a symbol (whose value
is a number), or a function (that evaluates to a number).  When passed
a symbol or function, it is evaluated at runtime for dynamic duration.

The function returns immediately with value DEFAULT when called the
first time.  On future invocations, the result from the previous call is
returned."
  (let ((debounce-timer nil)
        (delay-value (or delay 0.50)))
    (if (commandp func)
        ;; INTERACTIVE version
        (lambda (&rest args)
          (:documentation
           (concat
            (documentation func)
            "\n\nDebounce calls to this function"))
          (interactive (advice-eval-interactive-spec
                        (cadr (interactive-form func))))
          (prog1 default
            (if (timerp debounce-timer)
                (progn
                  (cancel-timer debounce-timer)
                  (timer-set-time
                   debounce-timer (time-add nil (timeout--eval-value delay-value)))
                  (timer-activate debounce-timer))
              (setq debounce-timer
                    (run-with-timer
                     (timeout--eval-value delay-value) nil
                     (lambda (buf)
                       (cancel-timer debounce-timer)
                       (setq debounce-timer nil)
                       (setq default
                             (if (buffer-live-p buf)
                                 (with-current-buffer buf
                                   (apply func args))
                               (apply func args))))
                     (current-buffer))))))
      ;; NON-INTERACTIVE version
      (lambda (&rest args)
        (:documentation
         (concat
          (documentation func)
          "\n\nDebounce calls to this function"))
        (prog1 default
          (if (timerp debounce-timer)
              (progn
                (cancel-timer debounce-timer)
                (timer-set-time
                 debounce-timer (time-add nil (timeout--eval-value delay-value)))
                (timer-activate debounce-timer))
            (setq debounce-timer
                  (run-with-timer
                   (timeout--eval-value delay-value) nil
                   (lambda (buf)
                     (cancel-timer debounce-timer)
                     (setq debounce-timer nil)
                     (setq default
                           (if (buffer-live-p buf)
                               (with-current-buffer buf
                                 (apply func args))
                             (apply func args))))
                   (current-buffer)))))))))