Created
January 21, 2016 00:01
-
-
Save guisouza/b1d4b4d624d4398965be to your computer and use it in GitHub Desktop.
Net
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
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