pah*_*nin 28 javascript settimeout
function slide()
{
if($('.current').is(':last-child')){
$('.current').removeClass('.current');
$('#imgholder').first().addClass('.current');
$('#imgholder').animate({left: '3920px'});
}
else{
$nxt=$(".current");
$(".current").removeClass("current");
$nxt.next().addClass("current");
$('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
}
}
var loop_handle= setTimeout("slide()",'3000');
Run Code Online (Sandbox Code Playgroud)
我已将此代码放在标题部分中,而setTimeout只运行一次
Rob*_*b W 69
setTimeout应该只运行一次.你在找setInterval.
var loop_handle = setInterval(slide, 3000);
Run Code Online (Sandbox Code Playgroud)
此外,第二个参数应该是数字,而不是字符串.当函数调用不需要任何参数时,最好引用函数而不是使用字符串.字符串将转换为函数.该功能将在窗口范围内执行.
setInterval("slide()", 3000);
//becomes
setInterval(Function("slide();"), 3000);
Run Code Online (Sandbox Code Playgroud)
是的,setTimeout 只运行一次。你要setInterval。此函数还返回可用于取消间隔的 ID。例如:
const slideInterval = setInterval(slide, 3000);
// later...
clearInterval(slideInterval);
Run Code Online (Sandbox Code Playgroud)
你只调用一次,所以它只执行一次.
也许你在考虑"setInterval()".
顺便说一句,当你调用它时,只需传递函数的名称而不是字符串:
setInterval(slide, 3000);
Run Code Online (Sandbox Code Playgroud)
该setTimeout功能只运行一次!如果你想多次运行它,你应该使用setInterval:
var loop_handle= setInterval("slide()",'3000');
Run Code Online (Sandbox Code Playgroud)
您也可以setTimeout在slide()函数的末尾再次使用重新设置超时:
var loop_handle;
function slide() {
if($('.current').is(':last-child')) {
$('.current').removeClass('.current');
$('#imgholder').first().addClass('.current');
$('#imgholder').animate({left: '3920px'});
}
else {
$nxt=$(".current");
$(".current").removeClass("current");
$nxt.next().addClass("current");
$('#imgholder').animate({left: '-=980'},{duration: 'slow', easing: 'easeOutBounce' });
}
loop_handle = setTimeout("slide()",'3000');
}
loop_handle = setTimeout("slide()",'3000');
Run Code Online (Sandbox Code Playgroud)