Skip to content

Commit

Permalink
Ping.js initial upload.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdfreder committed Feb 22, 2015
1 parent 1c9be25 commit ce393f5
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Creates and loads an image element by url.
* @param {String} url
* @return {Promise} promise that resolves to an image element or
* fails to an Error.
*/
var request_image = function(url) {
return new Promise(function(resolve, reject) {
var img = new Image();
img.onload = function() { resolve(img); };
img.onerror = function() { reject(url); };
img.src = url + '?random-no-cache=' + Math.floor((1 + Math.random()) * 0x10000).toString(16);
});
};

/**
* Pings a url.
* @param {String} url
* @return {Promise} promise that resolves to a ping (ms, float).
*/
var ping = function(url) {
return new Promise(function(resolve, reject) {
var start = (new Date()).getTime();
var response = function() {
var delta = ((new Date()).getTime() - start);

// HACK: Use a fudge factor to correct the ping for HTTP bulk.
delta /= 4;

resolve(delta);
};
request_image(url).then(response).catch(response);

// Set a timeout for max-pings, 5s.
setTimeout(function() { reject(Error('Timeout')); }, 5000);
});
};

0 comments on commit ce393f5

Please sign in to comment.