javascript - how to alert after all images loaded? -


im beginner in javascript i`m building javascript game , want alert after images loaded tried code not working

function loadeverything(){  var imgnumber = 0;  img1 = new image(); img1.src= "1.png" img1.onload = function(){ imgnumber =imgnumber+1; }   img2 = new image(); img2.src= "2.png" img2.onload = function(){ imgnumber =imgnumber+1; }   img3 = new image(); img3.src= "3.png" img3.onload = function(){ imgnumber =imgnumber+1; }  if(imgnumber==3)alert("done")  } 

the images loading asynchronously. checker code run before of images loaded. suggest checks for each image load. like:

function loadimages(urls,callback){    //counter   var counter = 0;    //checking function   function check(){     counter++;     if(urls.length === counter) callback();   }    //for each image, attach handler , load   for(var = urls.length; i--;){     img = new image();     img3.onload = check;     img.src= urls[i]   }  }  loadimages([/*array of urls*/],function(){   //this function fires when images have loaded }); 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -