Created
October 19, 2017 05:43
-
-
Save aloha1003/716cfee1a32b0433dd36e76c92719bf1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var isLoadingJob = false; | |
var startTime = {}; | |
var endTime = {}; | |
function pingByImage(host, img_path, pong) { | |
if (!isLoadingJob) { | |
isLoadingJob = true; | |
var img = host+img_path; | |
var hashId = hashCode(host); | |
var t = InitiateSpeedDetection(host, img); | |
t.then(function(message){ | |
pong(message.host, message.duration); | |
}).catch(function(reason){ | |
console.log(reason) | |
}); | |
; | |
} else { | |
window.setTimeout(pingByImage.bind(this,host, img_path, pong), 2000); | |
} | |
} | |
function InitiateSpeedDetection(host, src) { | |
return new Promise(function(resolve, reject) { | |
var download = new Image(); | |
download.onload = function () { | |
endTime[host] = (new Date()).getTime(); | |
var result = { | |
host: host, | |
startTime: startTime[host], | |
endTime: endTime[host], | |
duration:(endTime[host] - startTime[host]) / 1000, | |
}; | |
isLoadingJob = false; | |
resolve(result); | |
} | |
download.onerror = function (err, msg) { | |
reject(err); | |
} | |
startTime[host] = (new Date()).getTime(); | |
download.src = src+'?'+startTime[host] ; | |
}); | |
}; | |
/* var pong = function (host, m) { | |
console.log(host +" took "+m+" miliseconds."); | |
} | |
used : pingByImage('url', 'img_path', pong); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment