jquery - window.bind 函数导致 ie7 由于长时间运行的脚本而无法响应

标签 jquery windows internet-explorer-7

我将以下脚本用于响应式菜单。在 IE7 中,该脚本使页面卡住,并表示该页面“由于长时间运行的脚本而没有响应”。我发现导致卡住的位是代码底部的 window.bind 部分,根据我目前的研究表明它导致了 IE7 中的无限循环。我已经阅读了有关使用 setTimeout 等的答案,但我是新手,不知道如何将其实现到脚本中。有什么想法可以防止此脚本崩溃/卡住 IE7 吗?

这是一个涉及 this blog post 超时的解决方案, 但我不知道如何用下面的脚本实现它

/* Sample scripts for RWD nav patterns 
(c) 2012 Maggie Wachs, Filament Group, Inc - http://filamentgroup.com/examples/rwd-nav-   patterns/GPL-LICENSE.txt
Last updated: March 2012
Dependencies: jQuery
 */



jQuery(function($){

$('.nav-primary')
  // test the menu to see if all items fit horizontally
  .bind('testfit', function(){
        var nav = $(this),
            items = nav.find('a');

        $('body').removeClass('nav-menu');                    

        // when the nav wraps under the logo, or when options are stacked, display the nav as a menu              
        if ( (nav.offset().top > nav.prev().offset().top) || ($(items[items.length-1]).offset().top > $(items[0]).offset().top) ) {

           // add a class for scoping menu styles
           $('body').addClass('nav-menu');

        };                    
     })

  // toggle the menu items' visiblity
  .find('h3')
     .bind('click focus', function(){
        $(this).parent().toggleClass('expanded')
     });   

// ...and update the nav on window events
$(window).bind('load resize orientationchange', function(){
   $('.nav-primary').trigger('testfit');
});

});

最佳答案

我会查看 John Resig 的这篇文章 http://ejohn.org/blog/learning-from-twitter/

基本上,他建议不要将函数直接绑定(bind)到事件,而是让函数每 250 毫秒运行一次。

var outerPane = $details.find(".details-pane-outer"),
    didScroll = false;
$(window).scroll(function() {
    didScroll = true;
});

setInterval(function() {
    if ( didScroll ) {
        didScroll = false;
        // Check your page position and then
        // Load in more results
    }
}, 250);

如果浏览器同时触发许多事件,那会更有效率。您不会在页面开头运行 20 次调整大小事件。

关于jquery - window.bind 函数导致 ie7 由于长时间运行的脚本而无法响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10762364/

相关文章:

javascript - 字符串修改

c - Windows (64) 内核驱动程序和可分页功能

internet-explorer-7 - 当嵌入父容器中并溢出 :auto set 时,JQueryUI Accordion 在 IE7 中损坏

javascript - 如何从 github 引用远程文件路径?

javascript - Twitter Bootstrap 安装问题

java - 库路径不应该默认为工作区路径吗?

ajax - jQuery AJAX 超时后 IE7 中出现 "The data necessary to complete this operation is not yet available"消息

jquery - 如何使用 jQuery 获取 IE7 中元素的边框宽度?

javascript - 数据表+服务器端处理+搜索过滤

windows - 强制关闭正在运行的 dotnet 进程