JQuery - randomly call slideUp and slideDown in chain

I have the following code where I loop through img elements and want to randomly apply jQuery slideUp () and slideDown () effects to them:

$c.each(function(){
    $(this).find('img:gt(0)').hide();

    setInterval(function () {           
        $(this).find(':first-child')
         .[sometimes slideUp(), sometimes slideDown()] // <<<
         .next('img')
         .fadeIn()
         .end()
         .appendTo(this)
     }.bind(this), 3000 + Math.random()*4000); // 4 seconds
});

      

I cannot place functions in an array as they exist as methods of object objects. I also tried extending the item object to add my properties, but it didn't work either and didn't do a few more methods.

So, any ideas on how I can perform two different callbacks in turn, or just by chance?

+3


source to share


1 answer


You can refer to properties using array syntax. Therefore, it ["slideUp"]()

will call .slideUp()

.

You can also expand on this by adding a larger expression inside:



[ Math.random() > 0.5 ? "slideUp" : "slideDown" ]()

      

+7


source







All Articles