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 in JQuery by gaurav
• 13,460 points
5 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 by rajatha
• 4,940 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,720 points
4,027 views
0 votes
1 answer

jQuery document ready function

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

answered Jun 1 in JQuery by Edureka
• 13,640 points
18 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 in JQuery by Edureka
• 13,640 points
42 views
0 votes
1 answer

jQuery document ready function

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

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

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

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

answered Jun 20 in JQuery by rajatha
• 4,940 points
2 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
383 views
0 votes
1 answer

How to use jquery with asp.net ajax?

If you weren't aware, Microsoft is planning ...READ MORE

answered Oct 15, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
174 views
0 votes
1 answer

Is 'sparkline' a method?

I suggest you to check 2 things That jquery.sparkline.js is actually ...READ MORE

answered Nov 9, 2018 in Apache Spark by Frankie
• 9,830 points
574 views
0 votes
1 answer

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

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

answered 2 days ago in JQuery by rajatha
• 4,940 points
38 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 in JQuery by rajatha
• 4,940 points
8 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP