[英]setTimeOut with Parameters in IE8 not working
Can some one tell me where I am going wrong here, I have tested it in Firefox and Chrome and it works okay just need it working in IE8 now. 有人可以告诉我这里我出错的地方,我已经在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);
Also tried... 还试过......
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);
The correct legacy way to "pass parameters" to a function that can't accept them is with a closure: 将参数传递给不能接受它们的函数的正确遗留方法是使用闭包:
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();
}
A newer alternative (not compatible with IE8 without a polyfill): 一种较新的替代方案(与没有polyfill的IE8不兼容):
setTimeout(doStuff.bind(null, this), 200);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
I've never found setTimeout
parameters to be particularly reliable, so I just do this: 我从来没有发现
setTimeout
参数特别可靠,所以我这样做:
var it = this;
setTimeout(function() { ... }, 200);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.