title: SetInterval in a for loop , passing parameters
body: <pre><code>    var flag = true;
       function changeColor (element) {
          if(flag==true){
             document.getElementById(NAME+element).style.background="red";
             flag=false;
         }
         else if (flag==false){
             document.getElementById(NAME+element).style.background="white";
             flag = true;
       }
     }
  
      var temp;
      for(var i=0;i&lt;elements_array.length;i++)
      {
        alert("VAL="+elements_array[i]);
        temp = elements_array[i];
  
        setInterval(function() { changeColor(temp); } ,300);
      }
  </code></pre>
  
  <p>The above code works only for the last value. I'm trying to change color a particular cell which i get from integer array(elements_array).</p>
  
  <p>In the alert i get the array values correctly ,say 2,5,8. But only 8th (last) one is changing the color.</p>
  
  <p>But if i hard code the values as the following all the cells change color.</p>
  
  <pre><code> setInterval(function() { changeColor(2); } ,300);
   setInterval(function() { changeColor(5); } ,300);
   setInterval(function() { changeColor(8); } ,300);
  </code></pre>
  
  <p>Any ideas on why the loop is not working? Thank you</p>