2012-05-25 23 views
1

我對響應菜單使用以下腳本。在IE7中,腳本會使頁面凍結,並說該頁面「由於長時間運行的腳本而沒有響應」。我發現造成凍結的位是代碼底部的window.bind部分,從我的研究到目前爲止,這表明它導致了IE7中的無限循環。我已閱讀關於使用setTimeout等的答案,但我很新手,不知道如何將其實現到腳本中。任何想法如何我可以防止此腳本崩潰/凍結IE7?window.bind函數導致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'); 
}); 

}); 

回答

2

我由John Resig的http://ejohn.org/blog/learning-from-twitter/

基本上查看這篇文章,他建議不要將你的功能直接綁定到具有每250ms運行功能的事件上。

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次調整大小事件。

+0

感謝您的信息,雖然我不知道我有知識來執行它與我有什麼。你能給我一個例子在腳本中使用setInterval嗎? – Andrew

相關問題