0

I have a long list of images and I want to check each image (if broken, it should return false; else, return true)

I wrote this code but the function of checking the urls always return me wrong results

 ...
 validImages=[];
 this.list.forEach(item=>{
    if(checkImage(item.url)){
       validImages.push(item);
    }
 });
 ...
 checkImage(url){
    let image = new Image();
    image.src = url;
    console.log(image.onerror);
    if(image.onerror){return false;}else{return true;}
 }

hint: I could find these two statements. It works but i couldn't run them in my function to return true/false value

    image.onerror = function (evt){alert("can't download")};
    image.onload = function (evt){alert("can be download")}; 

So any helps for this issue ?

EDIT

Here is my new code

...
validImages=[];
this.list.forEach(item=>{
   this.imageExists(item.img, function(exists){
      if(exists){ // exists get correct true/false values
          validImages.push(item); //push function not works
      }
   });
});

checkImage(url, callback){
    let image = new Image();
    image.src = url;
    image.onerror = function (evt){callback(false);};
    image.onload = function (evt){callback(true);};
}
Ali Abdulaal
  • 175
  • 2
  • 15

0 Answers0