画像があれば画像を読み込んで表示するということをしたいと思います。
また、ファイルが存在しなければ処理を止めずに次の画像を読むようにしたいとおもいます。
ファイルの有無チェックのために、XMLHttpRequestを使うことにしました。
しかし、エラーを検知してもプログラムが止まってしまいます。
止まらないようにする方法は無いでしょうか?
大元の処理
下記のように、画像があれば画像にONLoadを実行させるとうことをしようとしました。
js
1function AllEvent() { 2 d.Deferred; 3 4 checkImage().then( 5 // 存在する場合 6 () => { loadingProcess }, 7 8 // 存在しない場合 9 return d.reject(); 10 ) 11 return promise(); 12} 13``` 14 15下記のようにイベントを連続させてつねに監視させます。 16非現実的なので、もし方法があれば教えていただきたいのです。 17実際は中で大きな画像を使うので、setintervalは使えません。 18 19```js 20AllEvent() 21.then(AllEvent()) 22.then(AllEvent()) 23.then(AllEvent()) 24.then(AllEvent()) 25.then(AllEvent()) 26``` 27 28### 画像のローディングチェック 29画像の有無を確認するために使います。 30 31```js 32// ローディング画面を出すプロセス 33function checkImage(url) 34{ 35 new Promise(function(resolve, reject) 36 { 37 var xhr = new XMLHttpRequest 38 xhr.open('GET', url) 39 xhr.onload = functon () 40 { 41 if(xhr.status === 200) 42 { 43 return resolve(xhr.response) 44 } else { 45 reject(new Error(xhr.statusText)) 46 } 47 } 48 xhr.onerror = reject 49 xhr.send() 50 }) 51}; 52``` 53 54### 画像のOnLoad処理 55画像のOnLoad。 56 57```sh 58function loadingProcess($target, imagePath) { 59 var d = new $.Deferred; 60 $loadingImage.css('display', 'block'); 61 var image = new Image(); 62 image.onload = () => { 63 $target.css( 64 'background-image', 65 'url(' + image.src + ')' 66 ); 67 $loadingImage.css('display', 'none'); 68 d.resolve(); 69 } 70 image.src = __Domain + 'assets/images/sprites/' + imagePath; 71 72 return d.promise(); 73} 74```
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。