Skip to content

Instantly share code, notes, and snippets.

@aloha1003
Created October 19, 2017 05:43
Show Gist options
  • Save aloha1003/716cfee1a32b0433dd36e76c92719bf1 to your computer and use it in GitHub Desktop.
Save aloha1003/716cfee1a32b0433dd36e76c92719bf1 to your computer and use it in GitHub Desktop.
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