How do I make an image load synchronously?

后端 未结 5 1957
暖寄归人
暖寄归人 2020-12-06 10:21

I want to create an object that has an image property, but I want the contstructor to finish running only once the image is loaded. Or to describe this with code:

         


        
5条回答
  •  庸人自扰
    2020-12-06 11:18

    Put the dependent code in the callback. There is no other non-evil way.

    GraphicObject = Class.extend({
    
        //This is the constructor
        init: function(){
              this.graphic = new Image();
              this.graphic.onload = function ()
              {
                  // the rest of the ctor code here
              };
              this.graphic.src = 'path/to/file.png';
         }
    });
    

提交回复
热议问题