Last active
October 9, 2017 09:21
-
-
Save jittagornp/dc1bd3de2d04309fb8052b4409db89c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author jittagornp <http://jittagornp.me> | |
* create : 2017/10/09 | |
* | |
* ให้เชื่อมต่อไปที่ url ปลายทางทั้งหมด N urls แบบ Asynchronous HTTP GET โดยใช้ Image Object มาช่วย | |
* ถ้า url ปลายทางไหน ไม่สามารถเชื่อมต่อได้ภายใน 1 วินาที ให้หยุดการเชื่อมต่อนั้น | |
* กรณีที่เชื่อมต่อแล้ว ไม่ว่าจะสำเร็จหรือไม่สำเร็จครบ N urls ให้ redirect ไปที่ TARGET_URL | |
*/ | |
(function(urls, redirectUrl) { | |
var count = 0; | |
var len = urls.length; | |
for (var i = 0; i < len; i++) { | |
(function(src) { | |
var image = new Image(); | |
image.loaded = false; | |
image.onload = image.onerror = function() { | |
image.loaded = true; | |
if (++count >= len) { | |
location.href = redirectUrl; | |
} | |
}; | |
image.src = src; | |
var t = setTimeout(function() { | |
clearTimeout(t); | |
if (!image.loaded) { | |
image.src = null; | |
} | |
}, 1000); | |
})(urls[i]); | |
} | |
})([...], 'TARGET_URL'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment