Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery function setInterval

$(document).ready(function(){

    setInterval(swapImages(),1000);

    function swapImages(){

        var active = $('.active'); 
        var next = ($('.active').next().length > 0) ? $('.active').next() : $('#siteNewsHead img:first');

        active.removeClass('active');
        next.addClass('active');
    }
});

I have 13 images contained in a div. The first one has a class called active, which means it is displayed.

The swap images function selects the active image and hides it, and makes the next image active.

However, when the page loads, the function only works correctly once, rather than looping.

Any ideas?

like image 814
callum.bennett Avatar asked Jul 14 '11 12:07

callum.bennett


1 Answers

This is because you are executing the function not referencing it. You should do:

  setInterval(swapImages,1000);
like image 159
Nicola Peluchetti Avatar answered Sep 27 '22 17:09

Nicola Peluchetti