我試圖檢測一個數組中的所有圖像何時加載,然後調用一個函數。在所有瀏覽器中工作正常,但IE。img.onload在IE中提供無效參數
//Detect when all images load
for (var i = 0, cnt = 0; i < urls.length; i++){
var img = new Image();
if(nodeNames[i] == 'IMG'){
//the following line is the problem, according to IE
img.onload = myFunction;
img.src = urls[i];
}
else if(nodeNames[i] === 'DIV'){
setTimeout(myFunction(),1);
}else{
setTimeout(myFunction(),1);
}
}
function myFunction(){
//...lots of code
}
else{
loaded = 0;
for(var i = 0, len = slides.length; i < len; ++i){
slides[i].css({
'display':'none'
});
}
}
}
'的setTimeout(myFunction的,1);'也許?雖然這不會成爲它只能在IE上工作的原因。 – pimvdb
哇,真的有用。謝謝! –
考慮使用jQuery的Deferred對象。 –