jQuery - 如何循环函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9695929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery - How to loop the function?
提问by KnightMax
I want to make a basic fadein fadeout slideshow.I only make it auto run once. How to make it have a loop??
我想做一个基本的淡入淡出幻灯片。我只让它自动运行一次。如何让它有一个循环?
html
html
<img src="image1.jpg" class="img1">
<img src="image2.jpg" class="img2">
js
js
$(document).ready(function () {
function playslider(){
$('.img1').fadeIn(800).delay(800).fadeOut(800);
$('.img2').delay(1600).fadeIn(800).delay(800).fadeOut(800);
}
playslider();
});
回答by Abe Miessler
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.
这称为“回调函数”。这个想法是当你最后一次淡出完成时它会被调用。您可以在此处阅读更多内容。
回答by Control Freak
You can use good old Javascript:
您可以使用旧的 Javascript:
$(document).ready(function () {
function playslider(){
// code to execute
x = setTimeout("playslider()", 5000); //will loop every 5 seconds.
}
playslider(); //start it up the first time
});
回答by Belabu
The answer is:
答案是:
$(document).ready(function () {
function playslider(){
// code to execute
x = setTimeout(function(){playslider()}, 5000);
}
playslider();
});
回答by navid saba
it works good but if u change it in this way it works better
它运作良好,但如果你以这种方式改变它,它运作得更好
$('.img1').fadeIn(800).delay(800).fadeOut(800);
$('.img2').delay(2400).fadeIn(800).delay(800).fadeOut(800, playslider);
just increase dalay time to 2400(2400=800+800+800)
只需将 dalay 时间增加到 2400(2400=800+800+800)