Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to loop a function

how do i loop a function in Jquery?

here is my code

HTML <div class="show2"> <div class="one"> <img src="img/productImg.jpg"> <img src="img/productImg.jpg"> <img src="img/productImg.jpg"> </div> <div class="two"> <img src="img/productImgt.jpg"> <img src="img/productImgt.jpg"> <img src="img/productImgt.jpg"> </div> </div>

MY Jquery

<script> $(function(){ $('.show2 .one:gt(0)').hide(); setInterval(function(){ $('.show2 .one').fadeOut() .next('.two').fadeIn() .end().appendTo('.show2');}, 3000); }); </script>

2 Answers

$(document).ready(function(){
    setInterval(function(){
        $('body').animate({backgroundColor:"black"},3000).delay(10000).animate({backgroundColor:"white"},3000).delay(10000);
    },5000);
});

I used this.to.fade the.page in and.out continuously

If you want to execute a function multiple times, you can either create a regular loop and execute it X amout of times (like a for, or a while loop) or you can use setInterval to execute your function once every xx seconds. What is it that you want to do and whats not working? Here's a small example of how you can execute a function multiple times (and here's a jsfiddle with the code http://jsfiddle.net/fxqAH/1):

var myFunction = function(interval) {
    if(interval){
        $('body').prepend('<span style="color: red">Im being prepended from setInterval...</span><br/>');
    } else {
        $('body').prepend('<span style="color: blue">Im being prepended from a while loop...</span><br/>');
    }
}

setInterval(function(){ 
    myFunction(true);
}, 1500); 

var start = 0,
    stop   = 10;

while (start < stop)
{
  myFunction(false);
  start++;
}