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

jQuery setTimeout() Won't Repeat?

  • I'm trying to build a custom image rotator in jQuery. This is my JavaScript code:

    /* Begin Settings */
    var time_int = 1000;
    var container = '.images';
    var object = 'img';
    /* End Settings */

    function imageRotator()
    {
    var img_amt = $(container + \" \" + object + \":last-child\").attr(\"class\");
    var active = $(container + \" \" + object + \"[style!=display:none]\").attr(\"class\");
    var next = parseInt(active) + 1;
    $(container + \" \" + object + \".\" + active).fadeOut(function(){
    $(container + \" \" + object + \".\" + next).fadeIn();
    });
    }

    $(document).ready(function(){
    $(container + \" \" + object).hide();
    $(container + \" \" + object + \".1\").show();
    setTimeout(\"imageRotator()\",time_int);
    });


    However, for some reason, the setTimeout at the bottom performs the function once after one second, and then does not continue performing the event after another second. Here's it in action: http://www.johnshammas.com/demos/imagerotator/

    Can anyone tell me how I can make it repeat?