Why do you get a warning
As @RGraham noted in the comments, the js compiler assumes that the second parameter $.grep() is a callback function and executes asynchronously (at least this is syntactic). However, this is not true because the second function is actually a filter function. See API docs
A warning is usually issued Mutable Variable is accessible from closure when using the async function inside a for loop. That is because the whole for loop has one area. This means that at each iteration, you should get the same variable. Thus, the callback will receive the wrong identifiers, because level (being mutable) will be changed before the callback is called. Fortunately, this is not the case you are dealing with (because $ .grep is not async) :)
... could you explain to me whether this will really affect my code to execute?
No, such a warning will not affect the result of your code.
You can simply ignore the warning, but if you still want to avoid this, you can put the contents in a close.
for (x = 0; x < levels.length; x++) { (function(){ var level = levels[x]; var candlesOnLevel = $.grep(relevantCandles, function(candles, index) { return parseInt($(candles).css("top").replace(/px/, "")) === level; }); })(); }
nalinc
source share