Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to loop the function?

Tags:

jquery

I want to make a basic fadein fadeout slideshow.I only make it auto run once. How to make it have a loop??

html

<img src="image1.jpg" class="img1">
<img src="image2.jpg" class="img2">

js

$(document).ready(function () {
function playslider(){
        $('.img1').fadeIn(800).delay(800).fadeOut(800);
        $('.img2').delay(1600).fadeIn(800).delay(800).fadeOut(800);
}   
playslider();
});
like image 687
KnightMax Avatar asked Dec 05 '25 22:12

KnightMax


1 Answers

Try changing it to this:

$(document).ready(function () {
function playslider(){
        $('.img1').fadeIn(800).delay(800).fadeOut(800);
        $('.img2').delay(1600).fadeIn(800).delay(800).fadeOut(800, playslider);
                                                                 //^callback function
}   
playslider();
});

This is called a "callback function". The idea is that it gets called when your last fadeout completes. You can read more here.

like image 200
Abe Miessler Avatar answered Dec 07 '25 10:12

Abe Miessler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!