jquerysettimeouteacharrayiterator

jquery setTimeout inside each


i have some code similar to this, that moves inside some images... it works but it doesn't seem to respect timer

var i = 1;
var indexArray = array(1,2,3);
var timerx = new Array();

$( indexArray ).each(function( indexArraykey ) {

    function internalCallback ( i, indexArraykey ) {
        val = indexArray[indexArraykey];
        console.log("test " + i + val);
    }); 

    timerx[i] = setTimeout( internalCallback( i, indexArraykey ), i * 500000 );                     

    i++;

}); 

Solution

  • A few points :

    So it seems that what you want is in fact this :

    var indexArray = array(1,2,3);
    var timerx = [];
    $.each(indexArray, function( indexArrayValue, i ) {
        timerx.push(setTimeout(function(){
            console.log("test " + i + ' : ' + indexArrayValue);
        }, (i+1) * 500000));
    });