treehouse : what would you like to learn today?
Web Design Web Development iOS Development

jQuery run function in a loop?

  • Hi i'm wondering how to run a function repetedly for an infinite ammount of time, basically i'm trying to achieve "autoplay" with this function.

    $.fn.play = function(){
    var options = $.extend($.fn.qslider.defaultsSetting,$.fn.qslider.options);
    $('.qS-scene').each(function(index){
    $('img', this).delay(options.sceneDuration*index+options.scenePause).each(function(index){
    $(this).delay(options.itemDelay*index).each($.fn.moveIn)
    })
    })
    }


    I could make a separate function if it helps, if you want to see it in action (i'm not good @ describing) go here:
  • You could use setInterval, but I'm sure some smarter people would explain to you why it's not good and there's something better.

    Here's some more reading on it: http://wallofscribbles.com/2011/setinterval-the-sneaky-basterd-child-of-javascript/
  • I'd use the setTimeout instead
    x
    function yourFunction() {
    // content of yourFunction
    setTimeout("yourFunction()", speed);
    }
    yourFunction();