javascript不起作用的幻灯片
我试图创建一个包含5个图像的幻灯片,但javascript不起作用javascript不起作用的幻灯片,javascript,html,slide,Javascript,Html,Slide,我试图创建一个包含5个图像的幻灯片,但javascript不起作用 <body onload='slide(0)'> <div id='slide'> </div> </body> 这是因为您没有设置计时器的函数引用,而只是调用函数slide,结果被设置为未定义计时器的回调 尝试: 功能幻灯片(一){ document.getElementById('slide').style.backgroundImage='url(../im
<body onload='slide(0)'>
<div id='slide'>
</div>
</body>
这是因为您没有设置计时器的函数引用,而只是调用函数
slide
,结果被设置为未定义计时器的回调
尝试:
功能幻灯片(一){
document.getElementById('slide').style.backgroundImage='url(../img/'+i+'.jpg');
setTimeout(function(){/@user2798694函数引用的一个演示点是,您必须为函数调用设置一个字符串表示形式,或者为setTimeout函数设置一个函数引用。
function slide(i)
{
document.getElementById('slide')
.style.backgroundImage = 'url(../img/' + i + '.jpg)';
setTimeout(slide((i+1)%5), 3000);
}
function slide(i) {
document.getElementById('slide').style.backgroundImage = 'url(../img/' + i + '.jpg)';
setTimeout(function () { //<-- Set the reference here
slide((i + 1) % 5); //Get the mod
}, 3000);
}
function slide(i) {
document.getElementById('slide').style.backgroundImage = 'url(../img/' + i + '.jpg)';
setTimeout(slide.bind(this, (++i) % 5), 3000);//<-- Set the function context using bind with the argument as next value of i
}