我有这个函数,我想第一次执行它,而不需要等待超时5000。
如何在第一次执行setTimeout函数时不延迟?
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));
}
function slideSwitch() {
var $gallery = $('#slideshow'),
$active = $gallery.find('img:visible'),
$next = $active.next().length ? $active.next() : $gallery.find('img').first();
var fooBar = function() {
$active.hide();
$next.fadeIn('1000', slideSwitch);
};
fooBar();
setTimeout(fooBar, 5000);
};