Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sundry Clojurescript snippets
;; milliseconds since the epoch
(.now js/Date)
;; read clojure data received over the network
(ns foo.core (:require [cljs.reader :as reader]))
(reader/read-string x)
;; write clojure data to send over the network
(pr-str {:hi "there"})
;; write something to the browser javascript console
(js/console.log x)
(.log js/console x)
;; working with websockets
(def websocket (atom (new js/WebSocket "ws://localhost:1234")))
(doall
(map #(aset @websocket (first %) (second %))
[["onopen" (fn [] (js/console.log "OPEN"))]
["onclose" (fn [] (js/console.log "CLOSE"))]
["onerror" (fn [e] (js/console.log (str "ERROR: " e)))]
["onmessage" (fn [m] (js/console.log (str "MSG: " (.-data m))))]]))
;; clojure data structure to js data structure
(let [mymap {:a "a" :b "b"}]
(.log js/console (clj->js mymap)))
;; delay execution (w/out using core.async)
(js/setTimeout #(swap! an-atom assoc :a "a") 2000)
;; select dom element by id
(defn by-id [id]
(.getElementById js/document id))
;; prevent the google closure compiler from munging a function name
(defn ^:export myfunc [] ... )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.