【发布时间】:2016-03-19 17:59:31
【问题描述】:
我正在将图像加载到 DOM 中
加载图像集和间隔的函数,并在执行另一个更改图像颜色的函数之前检查要完全加载的图像。
当文档加载时,我执行了 4 次这个函数(4 个不同的图像),这导致间隔永远不会在至少一个图像上被清除,有时是多个。
功能:
function setCloth(clothImg, prefix){
$('#'+prefix+'_cloth_div').html('<img class="" id="'+prefix+'_clothImg" src="/figures/'+clothImg+'" />');
cloth = prefix+'Cloth';
imageSource[cloth] = '/figures/'+clothImg;
checkClothInterval = setInterval(function(){
if (document.getElementById(prefix+"_clothImg").complete){
console.log('skin image complete');
changeCloth(prefix, 'Cloth', '#ffffff');
//====================================================
//somehow this is not reached on a couple of the image loads
//====================================================
clearInterval(checkClothInterval);
}else{
console.log('skin image not complete');
}
}, 100);
}
它是变量范围还是什么?
【问题讨论】:
-
对局部变量使用
var关键字。
标签: javascript scope callback setinterval