试图使最简单的画廊。有一个按钮可以打开图像<button class = "play-pause">Play</ button>
。点击它开始setInterval
,但再次按下时需要停止。如何才能做到这一点?不能停止滑块
这里的沙盒的链接:http://jsfiddle.net/eL31q3eq/
我尝试:
document.querySelector('.play-pause').addEventListener('click', function() {
if(timer == null) clearInterval(timer);
this.innerHTML = "Pause";
timer = setInterval(function() {
i = (i < images.length - 1) ? ++i : 0;
img.src = images[i];
}, 1000);
timer = null;
});
可能重复[如何停止间隔功能(http://stackoverflow.com/questions/21311662/how-to-stop-interval-function) – s3lph 2015-03-31 21:58:17