SetInterval y ClearInterval jQuery - StackOverflow en IE7
-
26-10-2019 - |
Pregunta
Tengo un código jQuery que se mueve hacia arriba y hacia abajo continuamente un DIV que contiene una lista con logotipos. La animación se repite dentro de un intervalo establecido y todo parece estar funcionando como se esperaba en Chrome, Firefox e IE 9 y 8 (sin errores de consola).
Sin embargo, en IE7 recibo 'error de script' y el navegador se congela totalmente ... Creo que el setInterval y ClearInterval no funcionan correctamente o he estropeado mi código ...
Aquí está:
//<!------------------LOGOS ------------------>
// the automatic scroll start immediately
$(document).ready(function () { membersLogos() });
// set the interval after which to restart the animated scroll
$(function () {
var intervalID;
var resetTimer = function () {
if (intervalID) { clearInterval(intervalID) };
intervalID = setInterval(function () {
membersLogos();
}, 190000);
};
});
// set the interval after which to restart the animated scroll
function membersLogos() {
var pane = $('#mypane');
if ($('#mypane').length) {
pane.jScrollPane({
animateScroll: true, //added
animateDuration: 95000, //added - length each way in milliseconds
stickToTop: true,
//autoReinitialise: true,
enableKeyboardNavigation: true
});
var api = pane.data('jsp');
var logosHeight = parseInt($('#mypane .jspPane').height());
//listen to the x-axis scrolling event
pane.bind('jsp-scroll-y', function (event, pos_y, at_top, at_bottom) {
//we're at the bottom now lets scroll back to the top
if (at_bottom) {
api.scrollToY(0);
$(this).unbind(event); //added with edit
$(this).bind('jsp-scroll-y', function (event, pos_y, at_top, at_bottom) {
if (at_top) {
$(this).unbind(event);
api.scrollToY(logosHeight);
}
});
}
});
//initial animate scroll to the top
api.scrollToY(logosHeight);
}
}
¿Alguna idea o sugerencia como lo que está mal? ¡Gracias por adelantado! Vik
Solución
Ya que ha establecido su intervalo en exactamente La suma de las dos animaciones (190000 = 95000 * 2), no me sorprendería si la función de intervalo se disparara nuevamente antes de que se completara la animación, quizás haciendo que algo pisara otra cosa. Se verifica fácilmente: simplemente establezca su intervalo en 300000 o algo así y vea si el problema desaparece.
Para evitar la posibilidad de volver a disparar el intervalo demasiado pronto, en lugar de usar setInterval
(que casi siempre es más problemas de lo que vale), vería si jScrollPane
Le ofrece una devolución de llamada de finalización cuando se completa la animación. Si lo hace, use esa devolución de llamada de finalización para activar la próxima animación. Si no es así, probablemente buscaría agregarlo.