简体   繁体   English

setInterval和clearInterval无法按预期工作

[英]setInterval and clearInterval not working as expected

I have this function which acts as a loading box using setInterval to change the background images which creates a flashing effect. 我有此功能,它用作使用setInterval更改背景图像的加载框,从而创建闪烁效果。

 function loading() {
    clearInterval(start);

    var i = 0;
    function boxes() {

        in_loading = ".in_loading:eq("  + i + ")";
        $(".in_loading").css("background", "url(images/load_bar_green.png)  no-repeat");    
        $(in_loading).css("background", "url(images/load_bar_blue.png)  no-repeat");        

        if(i == 3) {
            i = 0;
        } else {
            i++;
        }

    }
    var start = setInterval(function() {
        boxes();
    }, 350);
}

But even with clearInterval if I click on it more than once the flashing goes out of order. 但是,即使使用clearInterval如果我单击它的次数超过一次,闪烁也将不正常。 I tried removing the boxes, hiding them but I can't seem to get the 'buffer' cleared? 我试着去掉盒子,把它们藏起来,但是我似乎无法清除“缓冲区”? Any ideas? 有任何想法吗?

The reason why it keeps flashing is because every time loading gets called it creates a new variable start , so clearInterval is actually doing nothing. 它不断闪烁的原因是因为每次调用loading时,它都会创建一个新变量start ,因此clearInterval实际上什么也不做。 You also shouldn't have the boxes function within loading because it is doing the same thing, creating a new boxes function every time loading is called. 您也不应在loading使用boxes函数,因为它在做同样的事情,每次调用loading都会创建一个新的boxes函数。 This will add a lot of lag the longer the script executes. 脚本执行时间越长,就会增加很多延迟。

var i = 0;
var start;
function loading() {
    clearInterval(start);

    start = setInterval(function() {
        boxes();
    }, 350);
}

function boxes() {

    var in_loading = ".in_loading:eq("  + i + ")";
    $(".in_loading").css("background", "url(images/load_bar_green.png)  no-repeat");    
    $(in_loading).css("background", "url(images/load_bar_blue.png)  no-repeat");        

    if(i == 3) {
        i = 0;
    } else {
        i++;
    }

}

Function declarations get "hoisted" to the top of their scope, this is what is messing the execution order. 函数声明被“提升”到其作用域的顶部,这就是在混乱执行顺序。 Check this: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ 检查以下内容: http : //javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/

The reason is every time you call loading it creates a new Interval or new var start . 原因是每次调用load都会创建一个新的Interval或新的var start So if you click it twice, then you have two things manipulating the same data. 因此,如果单击两次,则有两个操作相同数据的东西。 So you need to have the var start outside of the function and the clearInterval inside. 因此,您需要将var start放在函数外部,并将clearInterval放在内部。 So every time you call loading it clears the interval and creates a new one. 因此,每次调用加载时,它都会清除间隔并创建一个新间隔。

var i = 0;
var start;
function loading() {
    clearInterval(start);

    start = setInterval(boxes, 350);
}

function boxes() {

    in_loading = ".in_loading:eq("  + i + ")";
    $(".in_loading").css("background", "url(images/load_bar_green.png)  no-repeat");    
    $(in_loading).css("background", "url(images/load_bar_blue.png)  no-repeat");        

    if(i == 3) {
        i = 0;
    } else {
        i++;
    }
}

maybe you should take a look at this Jquery Plugin , it seems to manage intervals very well . 也许您应该看看这个Jquery插件,它似乎可以很好地管理时间间隔。

Jquery Timers Plugin jQuery计时器插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM