Javascript clearInterval没有销毁setInterval计时器

Javascript clearInterval没有销毁setInterval计时器,javascript,jquery,setinterval,Javascript,Jquery,Setinterval,我正在创建一个图像旋转器,它在两个图像之间循环,并具有很好的交叉淡入度。我使用的是setInterval,但出于性能原因,我希望在调整浏览器大小时停止计时器 首先,我定义函数和全局处理程序: var imageRotator; var getImageRotator = function() { if (imageRotator) clearInterval(imageRotator); // <-- not working it would seem? s

我正在创建一个图像旋转器,它在两个图像之间循环,并具有很好的交叉淡入度。我使用的是
setInterval
,但出于性能原因,我希望在调整浏览器大小时停止计时器

首先,我定义函数和全局处理程序:

var imageRotator;
var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotator); // <-- not working it would seem?
    setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};
它以9秒的间隔启动,但每次我调整浏览器的大小时,它都会添加一个新的计时器,并且它会不断地开始交叉衰减


我哪里出错了?

如果你做了
imageRotator=getImageRotator(),然后需要从
getImageRotator
函数返回计时器。
setInterval
方法中缺少
return
语句:

var imageRotator;
var getImageRotator = function() {
    if (imageRotator) {
        clearInterval(imageRotator); // <-- not working it would seem?
        return setInterval(function() {
            $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
        }, 9000);
    }
};
var成像旋转器;
var getImageRotator=函数(){
if(图像旋转器){

clearInterval(imageRotator);//如果执行了
imageRotator=getImageRotator();
,则需要从
getImageRotator
函数返回计时器。
setInterval
方法中缺少
return
语句:

var imageRotator;
var getImageRotator = function() {
    if (imageRotator) {
        clearInterval(imageRotator); // <-- not working it would seem?
        return setInterval(function() {
            $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
        }, 9000);
    }
};
var成像旋转器;
var getImageRotator=函数(){
if(图像旋转器){

clearInterval(imageRotator);//如果执行了
imageRotator=getImageRotator();
,则需要从
getImageRotator
函数返回计时器。
setInterval
方法中缺少
return
语句:

var imageRotator;
var getImageRotator = function() {
    if (imageRotator) {
        clearInterval(imageRotator); // <-- not working it would seem?
        return setInterval(function() {
            $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
        }, 9000);
    }
};
var成像旋转器;
var getImageRotator=函数(){
if(图像旋转器){

clearInterval(imageRotator);//如果执行了
imageRotator=getImageRotator();
,则需要从
getImageRotator
函数返回计时器。
setInterval
方法中缺少
return
语句:

var imageRotator;
var getImageRotator = function() {
    if (imageRotator) {
        clearInterval(imageRotator); // <-- not working it would seem?
        return setInterval(function() {
            $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
        }, 9000);
    }
};
var成像旋转器;
var getImageRotator=函数(){
if(图像旋转器){

clearInterval(imageRotator);//您不能从getimageRotatorFunction()返回


您不能从getimageRotatorFunction()返回


您不能从getimageRotatorFunction()返回


您不能从getimageRotatorFunction()返回


您实际上并没有清除间隔

您需要实际设置imageRotatorInterval,然后清除该值。如下所示:

var imageRotator;
var imageRotatorInterval;
var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotatorInterval); 
    imageRotatorInterval = setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};

您实际上并没有清除间隔

您需要实际设置imageRotatorInterval,然后清除该值。如下所示:

var imageRotator;
var imageRotatorInterval;
var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotatorInterval); 
    imageRotatorInterval = setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};

您实际上并没有清除间隔

您需要实际设置imageRotatorInterval,然后清除该值。如下所示:

var imageRotator;
var imageRotatorInterval;
var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotatorInterval); 
    imageRotatorInterval = setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};

您实际上并没有清除间隔

您需要实际设置imageRotatorInterval,然后清除该值。如下所示:

var imageRotator;
var imageRotatorInterval;
var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotatorInterval); 
    imageRotatorInterval = setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};

您忘记分配id。
让simple返回并使用它:

var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotator); // <-- not working it would seem?
    return setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};
var imageRotator = getImageRotator();
var getImageRotator=function(){
if(图像旋转器)

clearInterval(imageRotator);//您忘记分配id。
让simple返回并使用它:

var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotator); // <-- not working it would seem?
    return setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};
var imageRotator = getImageRotator();
var getImageRotator=function(){
if(图像旋转器)

clearInterval(imageRotator);//您忘记分配id。
让simple返回并使用它:

var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotator); // <-- not working it would seem?
    return setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};
var imageRotator = getImageRotator();
var getImageRotator=function(){
if(图像旋转器)

clearInterval(imageRotator);//您忘记分配id。
让simple返回并使用它:

var getImageRotator = function() {
    if (imageRotator)
        clearInterval(imageRotator); // <-- not working it would seem?
    return setInterval(function() {
        $('.imageRotator').animate({opacity: ($('.imageRotator').css("opacity") == 1) ? 0 : 1}, 500);
    }, 9000);
};
var imageRotator = getImageRotator();
var getImageRotator=function(){
if(图像旋转器)

clearInterval(imageRotator);//为什么会有否决票?我肯定这能解决问题。不知道。它肯定能帮你解决这个问题,然后再加上一个(:@remus只是一个提醒,如果答案对你有帮助,你可以点击答案旁边的勾号,帮助其他访问者确定解决你问题的答案。是的@mauris;)但直到宽限期结束。为什么会有否决票?我肯定这解决了问题。不知道。它肯定会帮你解决问题。然后再加上一个(:@remus只是一个提醒,如果答案对你有帮助,你可以点击答案旁边的勾号,帮助其他访问者确定解决你问题的答案。是的@mauris;)但直到宽限期结束。为什么会有否决票?我肯定这解决了问题。不知道。它肯定会帮你解决问题。然后再加上一个(:@remus只是一个提醒,如果答案对你有帮助,你可以点击答案旁边的勾号,帮助其他访问者确定解决你问题的答案。是的@mauris;)但直到宽限期结束。为什么会有否决票?我肯定这解决了问题。不知道。它肯定会帮你解决问题。然后再加上一个(:@remus只是一个提醒,如果答案对你有帮助,你可以点击答案旁边的勾号,帮助其他访问者确定解决你问题的答案。是的@mauris;)但直到宽限期结束。