Last active
August 29, 2015 14:00
-
-
Save Jimbly/11302619 to your computer and use it in GitHub Desktop.
Periodic beacon firing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tasty, tasty bacons.