2011-12-19 82 views
0

您好我有這個小腳本,有淡入/淡出效果更改背景圖片,clearInterval不會清除間隔

/*controla la velocidad de la animación*/ 
    var slideshowSpeed = 1000; 
    var transitionSpeed = 2000; 
    var timeoutSpeed = 500; 

    /*No tocar*/ 
    var interval; 
    var activeContainer = 1;  
    var currentImg = 0; 
    var animating = false; 
    var navigate = function(direction) { 
     // Si ya estamos navegando, entonces no hacemos nada! 
     if(animating) { 
      return; 
     } 
     currentImg++; 
     if(currentImg == photos.length + 1) { 
      currentImg = 1; 
     } 
     // Tenemos dos, uno en el que tenemos la imagen que se ve y otro d?nde tenemos la imagen siguiente 
     var currentContainer = activeContainer; 
     // Esto puedo optimizarlo con la funci?n modulo, y cambiar 1 y 2 por 0 y 1-> active = mod2(active + 1) 
     if(activeContainer == 1) { 
      activeContainer = 2; 
     } else { 
      activeContainer = 1; 
     } 
     // hay que decrementar el ?ndice porque empieza por cero 
     cargarImagen(photos[currentImg - 1], currentContainer, activeContainer); 
    }; 
    var currentZindex = -1; 
    var cargarImagen = function(photoObject, currentContainer, activeContainer) { 
     animating = true; 
     // Nos aseguramos que el nuevo contenedor está siempre dentro del cajon 
     currentZindex--; 
     /*if(currentZindex < 0) currentZindex=1;*/ 
     // Actualizar la imagen 
     $("#headerimg" + activeContainer).css({ 
      "background-image" : "url(" + photoObject + ")", 
      "display" : "block", 
      "z-index" : currentZindex 
     }); 
     // FadeOut antigua 
     // Cuando la transición se ha completado, mostramos el header 
     $("#headerimg" + currentContainer).fadeOut(transitionSpeed,function() { 
      setTimeout(function() { 

       animating = false; 
      }, timeoutSpeed); 
     }); 
    }; 


function iniciarPase(){ 
    var animating = false; 
    //ver la siguente 
    navigate("next"); 
    //iniciar temporizador que mostrará las siguientes 
    interval = setInterval(function() { 
     navigate("next"); 
    }, slideshowSpeed); 
} 
function pararPase(){ 
    var animating = true; 
    console.log('paramos la animación'); 
    interval = clearInterval(interval); 
} 
/*Gestion de pase de imágenes de fondo*/ 
$(document).ready(function() { 


    iniciarPase(); 


}); 

但功能pararPase()至極包含clearInterval表現似乎即使不工作的在<body onload="pararPase()">

任何想法我錯過了什麼?

+1

你不調用'pararPase'任何地方...和'VAR動畫= '在開始/停止方法中什麼都不做! – ManseUK 2011-12-19 11:05:22

+0

'pararPase()'中的'interval'的值是什麼?我懷疑這是一些範圍或時間問題(即'interval'沒有你認爲它的價值) – 2011-12-19 11:06:55

+1

當你說pararPase不起作用,你的意思是它根本沒有被調用?我當然注意到你現在似乎沒有在任何地方調用它......你想要顯示代碼在哪裏被調用嗎? – Chris 2011-12-19 11:07:48

回答

2

你的基本邏輯工作得很好,這裏是live test case

所以最有可能你不正確地綁定pararPase,例如試圖綁定爲document.ready()之外的點擊處理程序時,當按鈕還不存在 - updated test case來證明這一點。

另一種選擇是您的代碼中存在其他錯誤,請檢查Chrome瀏覽器JavaScript控制檯以查看是否屬於這種情況。

正如其他人在評論中提到的那樣,將clearInterval的返回值分配回變量是沒有意義的,但不是有害的:變量的值只是「undefined」。

編輯:有可能多次調用iniciarPase,這會導致多個定時器只有最後一個被清除。所以,爲了安全起見添加到您的函數:(這實際上是什麼二極管試圖在他回答說)

function iniciarPase(){ 
    var animating = false; 
    //ver la siguente 
    navigate("next"); 
    //iniciar temporizador que mostrará las siguientes 
    if (interval) 
     clearInterval(interval); 
    interval = setInterval(function() { 
     navigate("next"); 
    }, slideshowSpeed); 
} 
+0

這基本上是我doning ..我有一個像$('。stop')。點擊(pararPase)但不會工作(我會看到console.log但間隔不會清除) – 2011-12-19 12:22:23

+0

@Toni所以這意味着'iniciarPase'被多次調用。看我的編輯。 – 2011-12-19 12:32:46

0
clearInterval(interval); 
interval = setInterval(function() { 
    navigate("next"); 
}, slideshowSpeed); 
+0

eeehm?爲什麼?謝謝 – 2011-12-19 11:46:41