Skip to content

Instantly share code, notes, and snippets.

@robozevel
Created March 30, 2014 09:16
Show Gist options
  • Save robozevel/9870053 to your computer and use it in GitHub Desktop.
Save robozevel/9870053 to your computer and use it in GitHub Desktop.
Ensure a function is invoked only when online
_.mixin({
whenOnline: function(fn, context) {
return function() {
var args = arguments;
context = context || this;
if (navigator.onLine) {
fn.apply(context, args);
} else {
window.addEventListener('online', function online() {
fn.apply(context, args);
window.removeEventListener('online', online);
});
}
};
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment