Skip to content

Commit

Permalink
fix debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
dgknght committed Dec 25, 2023
1 parent 7126da7 commit eba62dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject com.github.dgknght/app-lib "0.3.8"
(defproject com.github.dgknght/app-lib "0.3.9"
:description "Library of commonly used functions for web app development"
:url "https://github.com/dgknght/app-lib"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
Expand Down
15 changes: 8 additions & 7 deletions src/cljs/dgknght/app_lib/dom.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@
function after a delay (300 milliseconds unless otherwise specified) if
the function is not invoked again during the delay (in which case case
the timer starts again and the given fn invoked after the delay)."
([f] (debounce f 300))
([f timeout]
(let [id (atom nil)]
(fn [& args]
(js/clearTimeout @id)
(reset! id (js/setTimeout #(apply f args)
timeout))))))
[f & [timeout]]
(let [timeout-id (atom nil)]
(fn [& args]
(when-let [id @timeout-id]
(js/clearTimeout id))
(reset! timeout-id
(js/setTimeout #(apply f args)
(or timeout 300))))))

0 comments on commit eba62dd

Please sign in to comment.