Created
February 25, 2015 21:07
-
-
Save tbl3rd/039d2fb27f35734dfeda to your computer and use it in GitHub Desktop.
Call f with args every ms milliseconds when document is visible.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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