Skip to content

Instantly share code, notes, and snippets.

@shaunlebron
Created March 16, 2025 03:06
Show Gist options
  • Save shaunlebron/0889c21eabf648a88c4893caeecd197f to your computer and use it in GitHub Desktop.
Save shaunlebron/0889c21eabf648a88c4893caeecd197f to your computer and use it in GitHub Desktop.
setTimeout and setInterval in clojure (java)
(ns user
(:import (java.util.concurrent Executors TimeUnit)))
(defn set-timeout [^Callable f ^long ms]
(doto (Executors/newScheduledThreadPool 1)
(.schedule f ms TimeUnit/MILLISECONDS)))
(defn set-interval [^Callable f ^long ms]
(doto (Executors/newScheduledThreadPool 1)
(.scheduleAtFixedRate f 0 ms TimeUnit/MILLISECONDS)))
(comment
(def ses (set-timeout (fn [] (println "HI")) 1000))
(def ses (set-interval (fn [] (println "HI")) 1000))
(.shutdown ses)
(.shutdownNow ses))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment