jquery function setInterval

0 votes
$(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?

Jun 20, 2022 in JQuery by gaurav
• 22,940 points
357 views

1 answer to this question.

0 votes

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

  setInterval(swapImages,1000);
answered Jun 20, 2022 by rajatha
• 7,480 points

Related Questions In JQuery

0 votes
1 answer

Error:jquery each loop return false not end the function

Hii @kartik, Returning from one function doesn't call as it's ...READ MORE

answered Jun 2, 2020 in JQuery by Niroj
• 82,820 points
5,688 views
0 votes
1 answer

jQuery document ready function

Posted in: Using jQuery Core $( document ).ready() A page ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,640 points
241 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

We can fix this error by using jQuery() . ...READ MORE

answered Jun 1, 2022 in JQuery by Edureka
• 13,640 points
2,132 views
0 votes
1 answer

jQuery document ready function

$( document ).ready() A page can't be manipulated ...READ MORE

answered Jun 2, 2022 in JQuery by Edureka
• 13,640 points
279 views
0 votes
1 answer

jQuery 'if .change() or .keyup()'

you can bind to multiple events by ...READ MORE

answered Jun 20, 2022 in JQuery by rajatha
• 7,480 points
146 views
0 votes
0 answers

Jquery Masonary and item filtering does not work together

I am using a small js filter ...READ MORE

Jul 25, 2022 in Web Development by gaurav
• 22,940 points
74 views
0 votes
1 answer

jquery - is not a function error

In Wordpress jQuery.noConflict() is called on the jQuery file ...READ MORE

answered Aug 4, 2022 in Web Development by rajatha
• 7,480 points
161 views
0 votes
2 answers

How can we pass a function as a parameter in java?

Java 8 and above Using Java 8+ lambda ...READ MORE

answered Aug 28, 2018 in Java by Daisy
• 8,120 points
536 views
0 votes
1 answer

Jquery validation plugin - TypeError: $(...).validate is not a function

The "$(...). validate is not a function" ...READ MORE

answered Jun 28, 2022 in JQuery by rajatha
• 7,480 points
5,320 views
0 votes
1 answer

jQuery $( function() {} ) and $(document).ready the same?

So technically they are both the same. Not ...READ MORE

answered Jun 13, 2022 in JQuery by rajatha
• 7,480 points
141 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP