简体   繁体   English

窗口调整大小事件在ie7中不断触发

[英]window resize event continually fires in ie7

Old Title: setTimeout throttle on window resize event in javascript continually fires in ie7 旧标题:javascript中的窗口调整大小事件的setTimeout节流在ie7中不断触发

I have the following script : 我有以下脚本

jQuery(document).ready(function(){
    throttleTest(); 
});

function throttleTest() {

    var throttleTimer,
        testCount = 1;

    jQuery(window).on({
        "resize": function(){
            clearTimeout(throttleTimer);
            throttleTimer = setTimeout(function() {
                jQuery("ul.testList").prepend("<li>Prepended list item number: " + testCount + "</li>");
                testCount++;
            }, 500);        
        }
    });

};

And the following HTML : 以下HTML

<ul class="testList">
</ul>

Using a setTimeout throttle technique it should only prepend a list item to the testList ul once the user has stopped resizing their browser for 500ms. 使用setTimeout限制技术,只有在用户停止调整浏览器500ms后才应将列表项添加到testList ul。 Basically it only runs the setTimeout code once on every resize of the browser, due to the clearTimeout prior to it being set. 基本上它只在浏览器的每次调整大小时运行一次setTimeout代码,因为它在设置之前是clearTimeout。 This technique allows code to only be fired when needed and not on every resize event, which could be tens of times whenever the user resizes their browser. 这种技术允许代码仅在需要时触发,而不是在每次调整大小事件时触发,每当用户调整浏览器大小时,这可能是几十次。

This works across all browsers except for ie7. 这适用于除ie7之外的所有浏览器。 Bizarrely in ie7, the code continues to run and ceases to stop prepending list items to the ul. 奇怪的是,在ie7中,代码继续运行并停止将列表项前置到ul。

I have set up a demo here: http://jsfiddle.net/cQRjp/ 我在这里设置了一个演示http//jsfiddle.net/cQRjp/

Take a look in ie7 and you will see the issue. 看看ie7,你会看到问题。 Does anyone know why this is failing in ie7? 有谁知道为什么这在ie7失败了?

EDIT No:1: 编辑号:1:

I have stripped down the code so that on a window resize an li element gets prepended to a ul element on the page and then a counter is incremented. 我已经删除了代码,以便在窗口调整大小时,li元素会被预先添加到页面上的ul元素,然后计数器会递增。 That's it. 而已。

This has indicated that the problem lies with how ie7 interprets a resize event, nothing to do with the throttle timer. 这表明问题在于ie7如何解释调整大小事件,与节流定时器无关。 It seems that prepending an li item to the page triggers the resize event in ie7, therefore, the resize is continuously fired. 看起来在页面上添加li项会触发ie7中的resize事件,因此,会不断触发调整大小。 I have setup a new demo here: http://jsfiddle.net/gnKsE/ Warning this link will crash your ie7 browser. 我在这里设置了一个新的演示: http//jsfiddle.net/gnKsE/ 警告此链接会使你的ie7浏览器崩溃。

One solution I can think of to this problem is to turn off the resize event immediately after it is triggered, and then setting it back up again after I have ran the code within it. 我能想到的解决这个问题的一个解决方案是在触发后立即关闭resize事件,然后在我运行代码后再将其重新设置。 Like so: 像这样:

jQuery(document).ready(function(){
    functionName(); 
});

function functionName() {

    var throttleTimer,
        testCount = 1;


    function turnOnResize() {
        jQuery(window).on({
            "resize.anyname": function(){
                jQuery(window).off(".anyname");
                jQuery("ul.testList").prepend("<li>Resize event: " + testCount + "</li>");
                testCount++;
                setTimeout(function() {
                    turnOnResize();
                }, 50);
            }
        });
    }
    turnOnResize();

};

Another solution would be to make your resize handler check to see if the width of the window has changed. 另一种解决方案是检查调整大小处理程序以查看窗口的宽度是否已更改。 That way you can ignore resize events that were not caused by the window being resized. 这样,您可以忽略不是由调整大小的窗口引起的调整大小事件。 See also: window.resize event firing in Internet Explorer 另请参阅: Internet Explorer中的window.resize事件触发

Try something like this: 尝试这样的事情:

jQuery(document).ready(function($){
    var lastWindowHeight = window.innerHeight, // Could use $(window).height() but $(window) is expensive
        lastWindowWidth = window.innerWidth,
        testCount = 1;

    // Handles all resize events (which for IE7 includes when _anything_ in the DOM is resized)
    function resizeHandler() {
        if (lastWindowHeight !== window.innerHeight || lastWindowWidth !== window.innerWidth )
            windowResizeHandler.apply(this, arguments);
    }

    // Handles resize events that result from the window changing size
    function windowResizeHandler() {
        lastWindowHeight = window.innerHeight;
        lastWindowWidth = window.innerWidth;
        $("ul.testList").prepend("<li>Resize event: " + testCount + "</li>");
        testCount++;
    }

    $(window).on("resize", resizeHandler);
});

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

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