[英]setTimeOut with Parameters in IE8 not working
有人可以告诉我这里我出错的地方,我已经在Firefox和Chrome中测试了它,它运行正常,只需要它现在在IE8中工作。
setTimeout(function(it) {
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}, 200, this);
还试过......
function showmenu(it){
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}
window.setTimeout(function() {
showmenu(this)
}, 200);
将参数传递给不能接受它们的函数的正确遗留方法是使用闭包:
var that = this;
setTimeout(function(){ doStuff(that);}, 200);
function doStuff(it) {
x = $('.menuheader:first-child').position().left;
w = $('.menuheader:first-child').width();
p = x + w + 16;
$(it).next().css('left', p);
$(it).next().show();
}
一种较新的替代方案(与没有polyfill的IE8不兼容):
setTimeout(doStuff.bind(null, this), 200);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
我从来没有发现setTimeout
参数特别可靠,所以我这样做:
var it = this;
setTimeout(function() { ... }, 200);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.