Your problem is, that when the callback of the setTimeout() is finally executed, all calls of that callback refer to the same variable i, which has the same value for all instances.
Straight forward solution (submit i as a parameter to the callback; then each callback receives its own copy of i).
(The additional passing of parameters is not supported by IE<9 according to MDN, so we need a slightly modified version for those browsers.)
// IE9+ version
while(i--){
if(arr.indexOf(i) > -1){
setTimeout(function( index ){
check(index);
}, 1000, i );
}
}
// for IE below version 9
while(i--){
if(arr.indexOf(i) > -1){
!function( index ) {
setTimeout(function(){
check(index);
}, 1000 );
}( i );
}
}
Restructuring solution (here no callbacks inside a loop are needed and hence the problem does not exist):
setTimeout( function(){
while(i--){
if(arr.indexOf(i) > -1){
check(index);
}
}
}, 1000 );