This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Artemy Zemlyanika
Posted Dec 26 - Read on Facebook

Hey guys, another dummy question here:

for i in [0..4]
Utils.delay i, ->
print i # Output: 5 5 5 5 5

Why not "0 1 2 3 4"?


Mark Collette

Make sure print is indented one more level than Utils.delay

Artemy Zemlyanika

Mark Collette

Koen Bok

So to really understand why this happens (and is logical in js) you have to understand scoping/closures. Simply said; what parts of the program see what variables at any point. Here are some good explanations:

Mark Collette

It looks like i is captured by reference, so as it updates the closure accesses the latest value, and since the closure is invoked after a delay, then its after i has iterated beyond the range of 0 to 4. Likely you'll want a line between for and delay that captures the value of i into a different const, and use that const in the closure.

Read the entire post on Facebook