没有命名空间污染的ResizeStop

时间:2011-01-27 02:37:49

标签: javascript jquery resize closures

我正在尝试检测浏览器何时停止调整大小。以下工作,但取决于全球变量。有没有办法使用闭包来避免这种情况?如果是这样,怎么样?

    var rtime = new Date(1, 1, 2000, 12,00,00);
    var timeout = false;
    var delta = 200;

    $(document).ready(function() {          

        $(window).resize(function() {
            rtime = new Date();
            if (timeout === false) {
                timeout = true;
                setTimeout(resizeend, delta);
            }
        });

    });

    function resizeend() {
        if (new Date() - rtime < delta) {
            setTimeout(resizeend, delta);
        } else {
            timeout = false;
            $("#log").append("<span>resizeend</span> ");
        }               
    }

**脚本信用:http://vidasp.net/resizeend.html

1 个答案:

答案 0 :(得分:0)

(function(){ 
var rtime = new Date(1, 1, 2000, 12,00,00);
var timeout = false;
var delta = 200;

$(document).ready(function() {          

    $(window).resize(function() {
        rtime = new Date();
        if (timeout === false) {
            timeout = true;
            setTimeout(resizeend, delta);
        }
    });

});

function resizeend() {
    if (new Date() - rtime < delta) {
        setTimeout(resizeend, delta);
    } else {
        timeout = false;
        $("#log").append("<span>resizeend</span> ");
    }               
}
})();

是一个闭包