Javascript - wait images to be loaded

前端 未结 6 756
栀梦
栀梦 2021-01-07 03:46
var l = false;
var l2 = false;

var imm = new Image();
imm.src = \"b.png\";

imm.onload = function(){
l = true;
}

var imm2 = new Image();
imm2.src = \"c.png\";

imm         


        
6条回答
  •  不思量自难忘°
    2021-01-07 04:40

    If these are the only 2 times throughout your entire script that l and l2 are set, then you can check after setting the values.

    var l = false;
    var l2 = false;
    
    var imm = new Image();
    imm.src = "b.png";
    
    imm.onload = function(){
      l = true;
      if (l && l2) {
        myFunc();
      }
    }
    
    var imm2 = new Image();
    imm2.src = "c.png";
    
    imm2.onload = function(){
      l2 = true;
      if (l && l2) {
        myFunc();
      }
    }
    
    function myFunc(){
      //do stuff
    }
    

提交回复
热议问题