Skip to content

Instantly share code, notes, and snippets.

@tbl3rd
Created February 25, 2015 21:07
Show Gist options
  • Save tbl3rd/039d2fb27f35734dfeda to your computer and use it in GitHub Desktop.
Save tbl3rd/039d2fb27f35734dfeda to your computer and use it in GitHub Desktop.
Call f with args every ms milliseconds when document is visible.
(defn call-periodically-when-visible
"Call f with args every ms milliseconds when document is visible."
([ms f]
(letfn [(call [id]
(let [id (if (.-hidden js/document)
(and id (js/clearInterval id) nil)
(js/setInterval f ms))]
(goog.events/listenOnce
js/document "visibilitychange" #(call id))))]
(call nil)))
([ms f x & args]
(call-periodically-when-visible (fn [] (apply f x args)) ms)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment