Search code examples
clojurescriptgoogle-closure-library

Recursive Call with goog.dom.animationFrame


I am trying to use google closure animationFrame feature. I would like to create an animation task with it and to call that created task recursively.

I defined a def named animationTask. When I try to use that def recursively in that task it fails. It logs out that animationTask is undefined and thus can not be used as a function.

enter image description here

Could anyone point me in the right direction please? I feel like I am missing some basic clojure knowledge here.


Solution

  • Your code is calling the animation task function before it is defined. It is analogous to this simpler code:

    (defn create [x] (fn []))
    
    (def task (create {:measure (task)}))
    

    If you try that in a REPL, you'll see that task is being called while it is still undefined.

    Instead, the value under :measure is supposed to be a function, and the API takes a JavaScript object. This would be analogous to revising the above example to be:

    (def task (create #js {:measure (fn [state] (task))}))