2011-12-19 84 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