Created
October 14, 2014 11:00
-
-
Save athlan/5a2c3895f87e88cfe23f to your computer and use it in GitHub Desktop.
Callback once
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
var sampleCallback = function(arg1) { | |
console.log('callback fired: %s.', arg1) | |
} | |
var CallbackOnce = function(callback) { | |
this.isFired = false | |
this.callback = callback | |
} | |
CallbackOnce.prototype.create = function() { | |
var delegate = this | |
return function() { | |
if(delegate.isFired) | |
return | |
delegate.isFired = true | |
delegate.callback.apply(null, arguments) | |
} | |
} | |
obj1 = new CallbackOnce(sampleCallback) | |
var aa = obj1.create() | |
setTimeout(function() { | |
aa('a') | |
}, 200) | |
setTimeout(function() { | |
aa('b') | |
}, 500) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment