[英]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.