user2179977
user2179977

Reputation: 656

clojure: How do I use while/loop with sleep

I want to call a function every second.

I have the following code:

(defn dostuff []
  (do
    (print "I'm doing stuff")
    ...))

(while true
  (Thread/sleep 1000)
  (dostuff))

I would expect this to print "I'm doing stuff" every second but it does not.

How do I achieve this?

Upvotes: 1

Views: 2001

Answers (1)

Diego Basch
Diego Basch

Reputation: 13059

You're not flushing stdout. You can either use println, which is print plus newline (forces auto-flush), or explicitly call flush like this:

(defn dostuff []
  (do
    ...
    (print "I'm doing stuff")
    (flush)))

Upvotes: 4

Related Questions