javascript - image.Onload does not complete the image -


This is driving me crazy.

I need to load an array of images in javascript, but I have to make sure that all the images are loaded before the start of the drawing. Therefore, I am busy - the load event for each image is called. First of all, I create images and set their source and onload function:

( Load images {this.imagesArray [i] = new image (); This.imagesArray [i] .onload = this.onload (i); This.imagesArray [i] .src = Picture [i]; }

This is an onload function, the member of that class I was developing (the first two were in the stage maker):

  MyClass.prototype OnLoad = function (nm) {console.log ("the image is complete?", Nimaz, "", these images are [nimage] .complete); This.imagesLoaded ++;  

}

Then I'm busy to increase the counters for all on-load functions (again, in the constructor):

 < Code> while (this.images loaded & lt; this.nImages) {// Wait it is busy, and I do not like it. to continue; }  

So far, so good. But when I try to draw it on an HTML5 canvas with my draw class:

  MyClass.prototype.refresh = function () {// one of the images in the category Get var imageNum = getImageNum (); // Testing for perfection gives it a liar :( console.log ("Completion for image number", image is numerical, "is:", it's images. [ImageNum] .complete); this.drawClass.draw (this. ImagesArray [imageNum], this.xOrigin, this.organ);}  

console.log gives line false and I get the notorious NSIRRONOVAAVEAPLE exception.

Please do not do

What am I missing here?

Assign onload before setting the source It needs to be done, otherwise the script may be completed before setting the headler, perhaps it already fixes it.

Again in the busy waiting, it really does It's never a good idea. It is difficult to suggest alternatives, because you are not showing why you need to wait in the first but can be a good idea to find out if I am What's more, to learn onload being enhanced handler e array went full, and if it is, so to start the proceedings - which will create unnecessary busy waiting.


Comments