Skip to content

Instantly share code, notes, and snippets.

@guisouza
Created January 21, 2016 00:01
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 guisouza/b1d4b4d624d4398965be to your computer and use it in GitHub Desktop.
Save guisouza/b1d4b4d624d4398965be to your computer and use it in GitHub Desktop.
Net
Net = function(){
return this.constructor.apply(this,arguments)
}
Net.prototype.constructor = function (navigator) {
this.navigator = navigator;
// this.status = this.navigator.onLine;
this.events = [];
this.timer = setInterval(function(){
this.check()
}.bind(this),200)
};
Net.prototype.check = function () {
this.setStatus(this.navigator.onLine);
};
Net.prototype.setStatus = function(status){
this.status = status;
if (!status){
this.trigger('offline',status)
}else{
this.trigger('online',status)
}
this.trigger('change',status)
}
Net.prototype.on = function (event,callback) {
if(!this.events[event]){
this.events[event] = [];
}
this.events[event].push(callback);
};
Net.prototype.trigger = function (event,value) {
if (this.events[event]){
this.events[event].forEach(function(action){
action(value)
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment