Skip to content

Instantly share code, notes, and snippets.

@timblair
Created January 9, 2009 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timblair/45154 to your computer and use it in GitHub Desktop.
Save timblair/45154 to your computer and use it in GitHub Desktop.
Simple JS which polls a given script eveny X seconds, using an image rather than Ajax-style request.
Pinger = function(opts) { for (var o in opts) { this.config[o] = opts[o]; } }
Pinger.prototype = {
running: false,
config: {
host : document.location.host,
secure : (document.location.protocol == "https:"),
script : '',
interval : 300 // interval time in seconds
},
start: function() { this.running = true; this.schedule(); },
stop: function() { this.running = false; },
schedule: function() { window.setTimeout(this.poll(), this.config.interval * 1000); },
poll: function() { var scope = this; return function() { return scope.ping.apply(scope); } },
ping: function() {
if (!this.running) { return; }
new Image(1,1).src = "http" + (this.config.secure ? 's' : '') + "://"
+ this.config.host + this.config.script + "?" + Math.floor(Math.random()*9999999);
this.schedule();
}
};
/**
* Usage:
* var p = new.Pinger({host: 'mydomain.com', script: '/path/to/script.xxx', interval: 60});
* p.start();
* p.stop();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment