forked from jdfreder/pingjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.js
37 lines (33 loc) · 1.18 KB
/
ping.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
});
};