Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Created September 27, 2015 21:34
Show Gist options
  • Save qetr1ck-op/a292315fd0ddd6477827 to your computer and use it in GitHub Desktop.
Save qetr1ck-op/a292315fd0ddd6477827 to your computer and use it in GitHub Desktop.
/*
1. add defer() to all function prototypes
2. should wait for n second, then invoke a function
3. (!) and return a function
function f() {
console.log( a + b );
}
f.defer(1000)(1, 2); //after delay 1sec, 3
*/
Function.prototype.defer = function(ms) {
return function() {
var args = arguments;
setTimeout(function() {
this.apply(this, args);
}.bind(this), ms);
}.bind(this)
}
/*
in ES6
Function.prototype.defer = function(ms) {
var self = this;
return function() {
setTimeout(() => {
self.apply(this, arguments);
}, ms);
}
}
*/
function f(a, b) {
console.log( a + b );
}
f.defer(1000)(1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment