我正在使用以下脚本作为响应式菜单。在IE7中,脚本使页面冻结,并显示页面"由于长时间运行的脚本而没有响应"。我发现导致冻结的位是代码底部的window.bind
部分,到目前为止的研究表明,它导致了IE7中的无限循环。我已经阅读了有关使用 setTimeout 等的答案,但我非常新手,不知道如何在脚本中实现它。任何想法如何防止此脚本崩溃/冻结IE7?
这是这篇博文中涉及超时的解决方案,但我不知道如何使用下面的脚本实现它
/* 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/
基本上,他建议不要将函数直接绑定到事件,而是每 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);
当浏览器同时触发许多事件时,这将更有效率。您不会在页面开头运行 resize 事件 20 次。