我在 IE7 和 IE8 上遇到了这个疯狂的 javascript 问题。下面的函数表达式加载一些图像。就是这样。非常简单。当直接调用该函数时,即 testmypatience()
,它会按预期工作,但如果我在超时内调用它,它将不会加载图像。该函数被调用,但图像不会加载。当由 jQuery 动画回调调用时,它也无法工作。
我已经尝试了所有方法,但无法使其正常工作,因此我们将非常感谢您的帮助。
var testmypatience = function($active){
var arr = ['images/site/products-medium/m_cordial_pomegranateelderflower.png', 'images/site/products-medium/m_cordial_spicedberry.png', 'images/site/products-medium/m_cordial_strawberryelderflower.png', 'images/site/products-medium/m_750presse_coxsapple.png', 'images/site/products-medium/m_party_appleandelderflower.png', 'images/site/products-medium/m_squeezy_blackcurrentandapple.png', 'images/site/products-medium/m_270presse_cranberryandorange.png', 'images/site/products-medium/m_270presse_elderflower.png', 'images/site/products-medium/m_270presse_gingerlemongrass.png'];
for (i = 0; i < arr.length; i++) {
var img, len;
img = new Image();
img.src = arr[i];
img.onload = function(){
console.log('done ' + this.src);
}
}
}
//this works
testmypatience()
//None of these work
setTimeout(function(){
testmypatience()
}, 400)
setTimeout(testmypatience, 400)
jQuery('elm').animate({left:'1000px'},
{
duration:200,
complete: testmypatience
});
最佳答案
您不需要像解决方案中那样复杂。只需交换src
和onload
的分配即可。
错误:
img = new Image();
img.src = arr[i];
img.onload = function(){ ... }
右:
img = new Image();
img.onload = function(){ ... }
img.src = arr[i];
关于javascript - 从 setTimeout 调用时,函数无法在 IE7 和 IE8 中正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166929/