niloofar
niloofar

Reputation: 2334

Thread/sleep in clojurescript

Hellow everyone.

This is a block of code in events.cljs file. There is a button on the page that I want when I click that, a text appears on the page and disappear after 3 seconds. Here I wanted to assoc the text to the db after clicking the button and dissoc it after 3 seconds but there is an error that Thread namespace not found, from Thread/sleep line. Can anyone help me how I should fix that please?

Thank you

(rf/reg-event-db
  ::niloofar
  (fn [db [_]]
    (do
      (assoc db :greeting "hi")
      (Thread/sleep 3000)
      (dissoc db :greeting))))

Upvotes: 1

Views: 301

Answers (1)

Eugene Pakhomov
Eugene Pakhomov

Reputation: 10717

You should create a new effect that dissocs the value and use it with :dispatch-later, something like:

(rf/reg-event-fx ::show
  (fn [{db :db} _]
    {:db             (assoc db :greeting "hi")
     :dispatch-later {:ms 3000 :dispatch [::-hide]}))

(rf/reg-event-db ::-hide
  (fn [db _]
    (dissoc db :greeting)))

Upvotes: 2

Related Questions