문제

I use the following code to hide and show a div every 1 sec

$(document).ready(function(e) {
    var acc = setInterval(function(){
        $("#element").fadeOut(function(){
            setTimeout(function(){
                $("#element").fadeIn()  
            }, 1000)    
        })
    }, 200)
})

$(window).load(function(){
    setTimeout(function(){
        clearInterval(acc);
    }, 10000)
})

The clearInterval() is not working as it should and I get the following error on Google Chrome:

Uncaught ReferenceError: acc is not defined

DEMO: http://jsfiddle.net/enve/4GWvM/

도움이 되었습니까?

해결책

There is a scope issue in your code, you have defined the acc variable locally in the context of the document ready handler and it's not defined in the context of load handler.

다른 팁

Try redefining your acc variable outside the .ready callback.

var _acc = function(callback) {
    return setInterval(callback, 1000);
};

var acc = null;

$(document).ready(function() {
    acc = _acc(function() {
        $("#element").fadeOut(function(){
            setTimeout(function(){
                $("#element").fadeIn()  
            }, 1000)    
        })
    });
});

...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top