![](/img/trans.png)
[英]HTML Form and global variables: Why doesn't it recognize the global variable?
[英]Function doesn't recognize global variable for play/pause slideshow
我正在尝试为我的javascript幻灯片创建播放/暂停按钮,但由于某些范围问题,该按钮已停止我的进度。 我通过单击元素play
在两个字符串值之间切换。 当功能slideshow
打印输出working
,它无法打印状态并执行该功能。
这是画廊的小提琴:
我将衷心感谢您的帮助
var state='play'
slideshow()
pause.onclick=function(){
console.log(state)
if (state=='play')
{
state='pause'
}
else{
state='play'
}
}
function slideshow(){
console.log('working')
if (state == 'play'){
console.log('play')
var pic='<img src="'+ images[slide]+'">'
gallery.innerHTML=pic
slide+=1;
console.log(slide)
if(slide-1==length){
slide=0
setTimeout(slideshow, 3000);
}
}
}
更新了小提琴 。
请检查控制台,您可以看到未定义的错误:
未捕获的ReferenceError:幻灯片未定义(...)
当您声明幻灯片变量时,您将获得:
未捕获的ReferenceError:未定义图库(...)
您必须声明slide
和gallery
变量:
var slide=0;
var gallery= document.getElementsByClassName('gallery')[0];
希望这可以帮助。
我在这里放了一个完整的小提琴。
由于以下几个原因,您的幻灯片放映最初无法正常工作:
检查滑动计数器达到最大值的时间不正确。 它必须是: if(slide==images.length){
。
你有一个}
在错误的地方。 如果里面有人只设置超时if
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.