无法通过清除超时暂停幻灯片放映
所以我正在学习 javascript,目前正在做一项作业,我需要创建一个带有暂停按钮的循环浏览多个图像的横幅,问题是,我似乎无法使用clearTimeout() 暂停它
var images = [];
var timer;
images[0] = 'destaque-home.png';
images[1] = 'destaque-home-1.png';
images[2] = 'destaque-home-2.png';
function changeImg(){
document.MoveBanner.src = images[i];
if(i < images.length - 1){
i++;
} else {
i = 0;
}
var timer = setTimeout("changeImg()", 1000);
console.log(timer);
}
function LeftArrow() {
i--;
if (i < 0){
i=2;
}
document.MoveBanner.src = images[i];
}
function RightArrow() {
document.MoveBanner.src = images[i];
i++;
if (i > 2){
i=0;
}
}
function PauseBut() {
clearTimeout(timer);
}
window.onload = changeImg;```
So I'm learning javascript and I'm currently working on an assignment where I need to create a banner that cycles through multiple images with a pause button, problem is, I can't seem to pause it with clearTimeout()
var images = [];
var timer;
images[0] = 'destaque-home.png';
images[1] = 'destaque-home-1.png';
images[2] = 'destaque-home-2.png';
function changeImg(){
document.MoveBanner.src = images[i];
if(i < images.length - 1){
i++;
} else {
i = 0;
}
var timer = setTimeout("changeImg()", 1000);
console.log(timer);
}
function LeftArrow() {
i--;
if (i < 0){
i=2;
}
document.MoveBanner.src = images[i];
}
function RightArrow() {
document.MoveBanner.src = images[i];
i++;
if (i > 2){
i=0;
}
}
function PauseBut() {
clearTimeout(timer);
}
window.onload = changeImg;```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
var Timer = settimeout(“ thackimg()”,1000);
因为您使用了
var
,您创建的变量与globalvar Timer < /代码>在代码顶部声明的变量。这意味着pausbut函数正在尝试使用错误的
计时器
来清除时间。而是使用全球范围
计时器
:timer = settimeout(“ thackimg()”,1000);
>var timer = setTimeout("changeImg()", 1000);
Because you used
var
, the variable you create is not the same as the globalvar timer
variable declared at the top of the code. That means that the PauseBut function is trying to clearTimeout with the wrongtimer
.To instead use the globally scoped
timer
:timer = setTimeout("changeImg()", 1000);