Note that there are some explanatory texts on larger screens.

plurals
  1. POJSHint error Don't make functions within a loop
    primarykey
    data
    text
    <p>I'm running some code through JSHint and I keep getting the following error:</p> <blockquote> <p>Don't make functions within a loop.</p> </blockquote> <p>I tried turning off the warning for 'About functions inside loops' off which does nothing to stop the error from being reported. I have decided to refactor the code, using JSHint's suggestions here, <a href="http://www.jshint.com/options/" rel="nofollow">http://www.jshint.com/options/</a> but I'm still getting the error. I was hoping that somebody could help me to refactor this code slightly so it will pass. Here's a copy of the function:</p> <pre><code>function setSounds(parent) { var i, l; parent.getElements('.sound').each(function (elem) { var soundEvents = []; if (elem.get('fk_click_sound')) { soundEvents.push('click'); } if (elem.get('fk_mouseover_sound')) { soundEvents.push('mouseenter'); } if (soundEvents.length !== 0) { for (i = 0, l = soundEvents.length; i &lt; l; i += 1) { elem.addEvent(soundEvents[i], (function () { return function (e) { FKSoundAIR(FKSoundStd[this.get('fk_' + e.type + '_sound')]); }; })(elem), false); } } }); } </code></pre> <p>I'm using MooTools. The purpose of this function is to pass a parent element and then apply sound event to all of the children with the class 'sound.' I'm using custom HTML attributes, such as 'fk_click_sound' to feed additional information to the function. I picked up this method of assigning a function within a loop from <a href="http://blog.jbrantly.com/2010/04/creating-javascript-function-inside.html" rel="nofollow">http://blog.jbrantly.com/2010/04/creating-javascript-function-inside.html</a>. </p> <p>Any suggestions or resources that you can point me to would be great. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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