Skip to content

Instantly share code, notes, and snippets.

@Jimbly
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save Jimbly/11302619 to your computer and use it in GitHub Desktop.

Select an option

Save Jimbly/11302619 to your computer and use it in GitHub Desktop.
Periodic beacon firing
<html>
<head>
<script>
var beacon = (function() {
function fireBeacon(url, cb) {
//debug('Firing beacon ' + url);
var beacon = new Image();
beacon.onload = function() {
cb();
};
beacon.src = url;
}
var base_url = 'http://thesilentb.com/files/blank.gif';
var times = [1,5,10,15,30,60]; // last time delta is repeated
var time_index = 0;
var time = 0;
function wait() {
var dt;
if (time_index >= times.length) {
dt = times[times.length-1] - times[times.length-2];
} else {
dt = times[time_index] - (times[time_index-1] || 0);
}
++time_index;
setTimeout(function () {
time += dt;
// Note: "time" here is not wall time, especially if the page was put to sleep, etc
fireBeacon(base_url + '?time=' + time, function () {
wait();
});
}, dt * 1000);
}
return {
start: wait
};
}());
beacon.start();
</script>
</head>
<body>
</body>
</html>
@Jimbly

Jimbly commented Apr 25, 2014

Copy link
Copy Markdown
Author

Tasty, tasty bacons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment