Jquery 在第一次运行时设置超时而不延迟?
我有这个函数,我希望在第一次执行它时不等待超时5000 如何在第一次执行setTimeout函数时不延迟Jquery 在第一次运行时设置超时而不延迟?,jquery,Jquery,我有这个函数,我希望在第一次执行它时不等待超时5000 如何在第一次执行setTimeout函数时不延迟 function slideSwitch() { var $gallery = $('#slideshow'), $active = $gallery.find('img:visible'), $next = $active.next().length ? $active.next() : $gallery.find('img').first(); set
function slideSwitch() {
var $gallery = $('#slideshow'),
$active = $gallery.find('img:visible'),
$next = $active.next().length ? $active.next() : $gallery.find('img').first();
setTimeout(function() {
$active.hide();
$next.fadeIn('1000', slideSwitch);
}, 5000);
};
定义一个函数而不是匿名函数,然后在setTimeout之前调用它
function fnc ...
fnc();
setTimeout(fnc, 1000);
在隐藏/淡入淡出操作后移动
setTimeout
调用:
function slideSwitch() {
var $gallery = $('#slideshow'),
$active = $gallery.find('img:visible'),
$next = $active.next().length ? $active.next() : $gallery.find('img').first();
$active.hide();
$next.fadeIn('1000', () => setTimeout(slideSwitch, 5000));
}
你是说
setInterval
<代码>设置超时仅运行一次。或者您只想让它运行两次?您可能还想提供一些解释。
function slideSwitch() {
var $gallery = $('#slideshow'),
$active = $gallery.find('img:visible'),
$next = $active.next().length ? $active.next() : $gallery.find('img').first();
$active.hide();
$next.fadeIn('1000', () => setTimeout(slideSwitch, 5000));
}