我如何 'reset' 调用方法?

How do I 'reset' a method on call?

大家好,

我目前在 javascript 上遇到一个问题。

我有一个基于此 http://jsfiddle.net/pdb4kb1a/2 的自动幻灯片循环。

现在我还在图像中添加了箭头,这样用户也应该能够自己来回移动。 这里的问题是,当我调用 onClick() 时,它继续重新调用 slideShow(n) 方法,没有 'interrupting' 也没有 'resetting' 另一个。 所以我最终得到的是一个循环调用,而另一个循环仍然是 运行,这会在单击箭头按钮后创建一个非常快速且迷失方向的循环。

如何在通话时重置我的 slideShow() 循环?

var imgArray = ['img/sleutels.jpg', 'img/naamborden.jpg', 'img/leer.jpg'];
var photoIndex = 2;                                                             
var photoId = document.getElementById('photoSlide');



function setIndex(n) {                          
    slideShow((photoIndex + n));                            
}


function slideShow(n) {
    photoId.className += "fadeOut";                                             
    setTimeout(appear, 500);                                                    
    photoIndex++;
    if (photoIndex == imgArray.length) {                                        
        photoIndex = 0; 
    }
    console.log(photoIndex);
   setTimeout(slideShow, 5000);                                                 
}


function appear() { 
    photoId.src = imgArray[photoIndex];     
    photoId.className = "";                     
}


slideShow();

亲切的问候

您是否尝试将幻灯片放映超时保存在变量中并在 onclick 中重置它?类似的东西:

var imgArray = [
    'http://placehold.it/300x200',
    'http://placehold.it/200x100',
    'http://placehold.it/400x300'],
    curIndex = 0;
    imgDuration = 3000;

var slideTimeout;

document.getElementById('nextButton').addEventListener('click', onNextClick);

function slideShow() {
    document.getElementById('slider').className += "fadeOut";
    setTimeout(function() {
        document.getElementById('slider').src = imgArray[curIndex];
        document.getElementById('slider').className = "";
    },1000);
    nextIndex();
    slideTimeout = setTimeout(slideShow, imgDuration);
}
slideShow();

function onNextClick(domEvent){
  if (slideTimeout)
    clearTimeout(slideTimeout);
  
  nextIndex();
  slideShow();
}

function nextIndex(){
  curIndex++;
    if (curIndex == imgArray.length) { curIndex = 0; }
}
#nextButton{
  background-color: #aaa;
  padding:4px 12px;
  margin-bottom: 20px;
}

#slider {
    opacity:1;
    transition: opacity 1s; 
    display: block;
}

#slider.fadeOut {
    opacity:0;
}
<span id="nextButton">NEXT</span>
<img id="slider" src="http://placehold.it/50x200">

还有一些代码需要更改,因为我不太喜欢您处理索引的方式,但它确实有效...您只需增强此代码以处理 "previous" 图像,您将拥有你想要什么 ;) 希望对你有帮助

为了更好的结构,我会嵌套超时,然后存储超时的 id 并清除它:

var appearId, showId;

function setIndex(n) {
    slideShow((photoIndex + n));
}

function slideShow(n) {
    photoId.className += "fadeOut";
    photoIndex++;

    if (photoIndex == imgArray.length) {
        photoIndex = 0;
    }
    console.log(photoIndex);

    if (appearId) { clearTimeout(appearId) }
    appearId = setTimeout(appear, 500);
}

function appear() {
    photoId.src = imgArray[photoIndex];
    photoId.className = "";

    if (showId) { clearTimeout(showId) }
    showId = setTimeout(slideShow, 4500);
}

slideShow();