Skip to content

Instantly share code, notes, and snippets.

@weissjeffm
Created November 15, 2010 22:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weissjeffm/701051 to your computer and use it in GitHub Desktop.
Save weissjeffm/701051 to your computer and use it in GitHub Desktop.
Clojure macro to insert timeouts into loops
;;Code rewriting macro
(defmacro loop-with-timeout [timeout bindings & forms]
`(let [starttime# (System/currentTimeMillis)]
(loop ~bindings
(if (> (- (System/currentTimeMillis) starttime#) ~timeout)
(throw (RuntimeException. (str "Hit timeout of " ~timeout "ms.")))
(do ~@forms)))))
;;example use of macro
(loop-with-timeout 60000 []
(if (not (.isTextPresent sel text))
(do (Thread/sleep 15000)
(recur))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment