I'm trying to make a sleep function in ClojureScript (w/ Reagent):
(ns cljweb.webpage
(:require [reagent.core :as reagent]))
(def temp-atom (reagent/atom 0))
(defn sleep [msec]
(js/setTimeout (fn []) msec))
(defn page []
[:div
[:p @temp-atom]
[:button
{:on-click
(fn []
(sleep 3000)
(swap! temp-atom inc))}
"Click me!"]])
For some reason, this doesn't sleep properly - when I click the "Click me!" button, temp-atom
increments instantly - when I time it, by putting this after in page
:
[:p (time (sleep 3000))]
I get this in the console:
"Elapsed time: 0.015000 msecs"
What did I do wrong in the code?
sleep
without usingsetTimeout
? That's my primary goal. I'm pretty sure that make my question an XY question, though, so I may have to post a new question. – Recent