-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (26 loc) · 748 Bytes
/
index.js
File metadata and controls
33 lines (26 loc) · 748 Bytes
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
'use strict';
var one = require('one-time');
/**
* Send a small beacon request.
*
* @param {String} url The URL we want to reach.
* @param {Function} fn Optional completion callback.
* @param {Number} timeout Optional timeout before callback is invoked.
* @returns {Image} Image instance that we used to request data.
* @api public
*/
module.exports = function beacon(url, fn, timeout) {
var img = new Image()
, time;
fn = one(fn || function nope() {});
img.onload = img.onerror = function cleanup() {
img.onload = img.onerror = null;
clearTimeout(time);
fn();
};
img.src = url;
time = setTimeout(function timeout() {
fn(new Error('Beacon request timed out'));
}, timeout || 1000);
return img;
};