我在我的代码中遇到了事件监听器和图像加载器的问题。当我尝试加载并检查基于事件侦听器加载的图像的状态时,跳过了一些图像。我认为浏览器只能处理一个事件,而当活动的事件不会停止时,可以工作并跳过其工作。因此很少的图像会返回到前景状态并可以使用。给我一些关于这个的建议。这是我的代码:AddEventListener和图像加载器的问题
//Image loader
var urlList = [
'images/ground_02.png', // 0 - FG
'images/ground_layer0.png', // 1 - CITY
...
];
var urlListLength = urlList.length; //length of URL-array
var iCount = new Number(0); // Create zero-counter
var images = new Array(); // All images array. Each elem stores one image
var imageInfo = function(imageName, imageCount, imageWidth, imageHeight) { // Constructor for image data container
this.imageName = imageName; // Url of image
this.imageCount = imageCount; // Frames
this.imageWidth = imageWidth; // Width of images
this.imageHeight = imageHeight; // Height of images
};
var imageData = new Array(); // Images data array
for (var i=0; i!=urlListLength; ++i) { // Loop for each image load
images[i] = new Image(); // Image array.
images[i].addEventListener('load', function(i) {
imageData.push(new imageInfo(images[iCount], images[iCount].width/images[iCount].height, images[iCount].width, images[iCount].height));
iCount++;
if (iCount == urlListLength) {
var loaded = true;
loop();
};
}, false);
images[i].src = urlList[i];
images[i].onerror=function() {
alert("FAIL "+this.src);
};
};
你可以编辑你的文章,使其更清晰你问什么问题?你有没有试图确认你对事业的想法? – Hbcdev
如何测试一些图像是否加载? –
你应该在设置src之前定义onerror *。 –