FireFox 在 setInterval 中没有 运行 代码
FireFox not running code in setInterval
这让我有点生气。我的 setInterval
在 Chrome、IE 和 Safari 上运行良好,但 FireFox 让我很失望。使用下面的代码,它没有设置我定位的 <li>
的高度 - 你能明白为什么不吗?
// For fullscreen overlay navigation
var uid = '#my-ul-id'
function osuFixLiHeights(targetUl) {
var t = $(targetUl);
var ch = $(t).height();
var lih = ch / 2;
$(t).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first as it seems this is needed to re-add the heights
setInterval(function () {
$(this).height(lih); // delay adding heights
},100);
});
}
osuFixLiHeights(uid);
编辑
感谢您到目前为止的帮助,非常感谢。更新了下面的代码——正如我的一条评论中提到的,这对于设置 <li>
的初始高度非常有用,我遇到的唯一突出的问题是当从 window 调整浏览器大小时从较小的尺寸到较大的尺寸,<li>
的高度设置正确,但是,使浏览器 window 变小不会改变 <li>
的高度。
这是我使用的代码:
var didResize = (function(){
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
function osuFixLiHeights(targetUl) {
var ch = $(targetUl).height();
var lih = ch / 2;
$(targetUl).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first
var targetLi = $(this);
setTimeout(function () {
$(targetLi).height(lih); // delay adding heights
}, 100);
});
}
$(window).resize(function() {
didResize(function() {
osuFixLiHeights('#menu-overlay-nav');
}, 500);
});
我认为问题出在 setInterval
函数的主体上。
尝试将 this
分配给变量:
$(t).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first as it seems this is needed to re-add the heights
var self = this; // Capture the current 'this' value
setInterval(function () {
// Here 'this' is not the same 'this' as before
$(self).height(lih); // delay adding heights
}, 100);
});
这让我有点生气。我的 setInterval
在 Chrome、IE 和 Safari 上运行良好,但 FireFox 让我很失望。使用下面的代码,它没有设置我定位的 <li>
的高度 - 你能明白为什么不吗?
// For fullscreen overlay navigation
var uid = '#my-ul-id'
function osuFixLiHeights(targetUl) {
var t = $(targetUl);
var ch = $(t).height();
var lih = ch / 2;
$(t).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first as it seems this is needed to re-add the heights
setInterval(function () {
$(this).height(lih); // delay adding heights
},100);
});
}
osuFixLiHeights(uid);
编辑
感谢您到目前为止的帮助,非常感谢。更新了下面的代码——正如我的一条评论中提到的,这对于设置 <li>
的初始高度非常有用,我遇到的唯一突出的问题是当从 window 调整浏览器大小时从较小的尺寸到较大的尺寸,<li>
的高度设置正确,但是,使浏览器 window 变小不会改变 <li>
的高度。
这是我使用的代码:
var didResize = (function(){
var timer = 0;
return function(callback, ms) {
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
function osuFixLiHeights(targetUl) {
var ch = $(targetUl).height();
var lih = ch / 2;
$(targetUl).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first
var targetLi = $(this);
setTimeout(function () {
$(targetLi).height(lih); // delay adding heights
}, 100);
});
}
$(window).resize(function() {
didResize(function() {
osuFixLiHeights('#menu-overlay-nav');
}, 500);
});
我认为问题出在 setInterval
函数的主体上。
尝试将 this
分配给变量:
$(t).find('li').each(function(i) {
$(this).removeAttr(); // clear heights first as it seems this is needed to re-add the heights
var self = this; // Capture the current 'this' value
setInterval(function () {
// Here 'this' is not the same 'this' as before
$(self).height(lih); // delay adding heights
}, 100);
});