From 0d127c0121a53ff4fbe607bb606df7e45641f00a Mon Sep 17 00:00:00 2001 From: MoyuScript Date: Fri, 18 Aug 2017 21:53:42 +0800 Subject: [PATCH] Correctly apply image timeout for loading images --- src/ImageLoader.js | 13 +++++++++++-- src/index.js | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/ImageLoader.js b/src/ImageLoader.js index 53e2ff7..d33a29f 100644 --- a/src/ImageLoader.js +++ b/src/ImageLoader.js @@ -78,10 +78,12 @@ export default class ImageLoader { } } }; - xhr.ontimeout = () => reject(`Timed out fetching ${src}`); xhr.responseType = 'blob'; if (this.options.imageTimeout) { - xhr.timeout = this.options.imageTimeout; + const timeout = this.options.imageTimeout; + xhr.timeout = timeout; + xhr.ontimeout = () => + reject(__DEV__ ? `Timed out (${timeout}ms) fetching ${src}` : ''); } xhr.open('GET', src, true); xhr.send(); @@ -122,6 +124,13 @@ export default class ImageLoader { resolve(img); }, 500); } + if (this.options.imageTimeout) { + const timeout = this.options.imageTimeout; + setTimeout( + () => reject(__DEV__ ? `Timed out (${timeout}ms) fetching ${src}` : ''), + timeout + ); + } }); this.cache[key] = diff --git a/src/index.js b/src/index.js index 6a841b6..1faf848 100644 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,7 @@ export type Options = { async: ?boolean, allowTaint: ?boolean, canvas: ?HTMLCanvasElement, - imageTimeout: ?number, + imageTimeout: number, proxy: ?string, removeContainer: ?boolean, scale: number,