views:

134

answers:

2

Hi, I am using http://mess.genezys.net/jquery/jquery.async.php for loop and need to reverse the order of the array(1,2,3) instead it goes from 3 to 1

Anyway to change that from the code? Thanks

A: 

Use this http://www.w3schools.com/jsref/jsref_reverse.asp reverse() function if that is what you need, maybe some code would be better explanation. cheers

c0mrade
+1  A: 
var someArray = [1,2,3],
    i = someArray.length - 1;

// Reverse processing
jQuery.whileAsync({
    delay: 100,
    bulk: 0,
    test: function() { return i >= 0 },
    loop: function() { 
        // Do something with the array
        i--;
    }
});

// Forward processing
i = 0;
var len = someArray.length;
jQuery.whileAsync({
    delay: 100,
    bulk: 0,
    test: function() { return i < len; },
    loop: function() { 
        // Do something with the array
        i++;
    }
});
Justin Johnson
Thanks. I think this may work but another question: I said array to make it simple. But if it is a $('.className') then how do I count the length of this array? in order to figure out iThanks again
HP
Oh I think I find the solution with .length as well (it works for selector). But how to use whileAsync in this case (having selector instead of array)?
HP
jQuery's `$` returns an array-like container. In other words, you can treat it just like an array. All you would have to do is change `someArray = [1,2,3]` to `someArray = $('.className')`
Justin Johnson
Sorry Justin for coming back to this but it doesn't work the way I think.var TimelineObj = $('div.#myobj'),len = TimelineObj.length,thisItem; I use loop in jQuery.whileAsync() but doing this caused errorthisItem = TimelineObj[i];someFunction(thisItem.find('.some-class'));i++What to fix here? Thanks
HP