如何在ClojureScript中覆盖onload方法?

时间:2016-01-10 13:48:48

标签: clojurescript

我正在尝试在ClojureScript中覆盖onloaddocument的{​​{1}}函数。我认为Image应该可以做到,但我没有取得任何成功。相关代码如下:

set!

(defn load-image [img-path] (let [img (js/Image.)] (do (set! (.-src img) img-path) img))) (defn add-img-canvas [img-path width height] (let [img (load-image img-path)] (set! (.-onload img) (fn [] ;; This function is never called. (let [canvas (get-scaled-canvas img width height)] (do (pr-str canvas) (swap! game-state :canvas canvas))))))) (defn hello-world [] (let [count (atom 1)] (fn [] [:div [:h1 (:text @game-state)] [:div (do (swap! count inc) (str "count is " @count))] [:canvas (:canvas @game-state)]]))) (reagent/render-component [hello-world] (. js/document (getElementById "app"))) (set! (.-onload js/document) (fn [] ;; This function is also never called. (add-img-canvas (:img-src game-state) 100 130))) ;;(. js/document onload) 中的匿名函数未被调用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为可能会归结为document.onloadwindow.onload之间的差异。后者确实按预期工作。

有关两者之间的更多详细信息,请参阅this