javascript - Changing the name of a html class with for loop variable -


i have several of html class's incrementing in class name such as:

<div class="chicken1">     <b></b> </div> <div class="chicken2">     <b></b> </div> <div class="chicken3">     <b></b> </div> 

i'm trying write loop loop through these class names, adding index end each class name , calling function in 2s delays.

for ( var = 1; <= 3; i++ ) {     settimeout(function() {         myfunction(".chicken" + + " b");     }, 2000 * i); } 

however isn't working.

fiddle

the problem of settimeout() called within loop; have close on loop variable:

for (var = 1; <= 6; ++i) {     settimeout((function(i) {         return function() {             myfunction(".chicken" + + " i");         };     })(i), * 2000); } 

demo

it uses function gets called immediately, passing value of i; value retained until settimeout() fired.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -