×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Mikhail Furlender
Added: Jan 12, 2017 4:44 PM
Views: 17
  1. var urlExists = function (url) {
  2.   var http = new XMLHttpRequest();
  3.   var dfd = new $.Deferred();
  4.   http.open('HEAD', url);
  5.   http.onreadystatechange = function () {
  6.       if (this.readyState == this.DONE) {
  7.           if (this.status != 404) {
  8.               dfd.resolve(url);
  9.           } else {
  10.               dfd.resolve(false);
  11.           }
  12.       }
  13.   }
  14.   http.send();
  15.   return dfd.promise();
  16. }
  17.  
  18. var deferreds = imgRange.map(function (o) {
  19.  return urlExists(o);//,pushTo);
  20. });
  21.  
  22. $.when.apply($, deferreds).done(function () {
  23.  for (var _len = arguments.length, images = [], _key = 0; _key < _len; _key++) {
  24.      if (arguments[_key]) {
  25.          images.push(arguments[_key]);
  26.      }
  27.  }
  28.   //images is now a filtered array of existing images, do what you please
  29.  
  30. });
  31.  
  32.