Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript parameter changing values?
    text
    copied!<p>I'm building a running light library on top of raphaeljs. In the code example below I'm trying to iterate through the set of circles in blocks of 5 and animate the blocks individually. </p> <p>What I don't understand is why x has a value of 1 in the first alert and becomes a value of 2 after passing it to the fade function. Any ideas why? I must be overlooking something by can not figure it out... Thanks!</p> <pre><code>Raphael(function() { var r = Raphael("Test"), run = document.getElementById("run"), set = r.set(r.circle(62, 100, 6), r.circle(125, 100, 3), r.circle(139, 100, 3), r.circle(153, 100, 3), r.circle(167, 100, 3), r.circle(181, 100, 3), r.circle(195, 100, 3), r.circle(209, 100, 3), r.circle(223, 100, 3), r.circle(237, 100, 3), r.circle(251, 100, 3), r.circle(265, 100, 3), r.circle(279, 100, 3), r.circle(293, 100, 3), r.circle(307, 100, 3), r.circle(321, 100, 3), r.circle(334, 100, 3), r.circle(348, 100, 3), r.circle(362, 100, 3), r.circle(376, 100, 3), r.circle(390, 100, 3)).attr({ stroke : "none", fill : "#e6e6e6" }), fade = function(obj, id, x) { alert('passed x = ' + x); return function() { obj[(5 * x) - id].attr({ fill : "#49719b", r : 4 }).animate({ fill : "#e6e6e6", r : 3 }, 400); }; }; run.onclick = function() { var i = 0; while (i &lt;= 0) { for (x = 1; x &lt;= 1; x++) { alert('initial x = ' + x); setTimeout(function() { set[0].animate({ "20%" : { callback : fade(set, 0, x) }, "40%" : { callback : fade(set, 1, x) }, "60%" : { callback : fade(set, 2, x) }, "80%" : { callback : fade(set, 3, x) }, "100%" : { callback : fade(set, 4, x) } }, 650); }, i * 650); } i++; } }; }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload