Created
March 30, 2014 09:16
-
-
Save robozevel/9870053 to your computer and use it in GitHub Desktop.
Ensure a function is invoked only when online
This file contains 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
_.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