Javascript - wait images to be loaded

前端 未结 6 794
栀梦
栀梦 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:26

    you don't have to constantly check, just check the other each time one finishes, that way, when the second is done your function will fire, and you will only have had to "check" once. var l = false; var l2 = false;

    var imm = new Image();
    imm.src = "b.png";
    
    imm.onload = function(){
    l = true;
    
    if (l2)
       call yourFunction();
    
    }
    
    var imm2 = new Image();
    imm2.src = "c.png";
    
    imm2.onload = function(){
    l2 = true;
    
    if (l)
       call yourFunction();
    }
    

提交回复
热议问题