Skip to content

Instantly share code, notes, and snippets.

@athlan
Created October 14, 2014 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save athlan/5a2c3895f87e88cfe23f to your computer and use it in GitHub Desktop.
Save athlan/5a2c3895f87e88cfe23f to your computer and use it in GitHub Desktop.
Callback once
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