I have a function that I want to subscribe it to a event queue. When it executes this function I need it to unsubscribe from the queue.
functionToExecute is the function I want to execute.
onDOMReady subscribes the attachDOMHandler function which in turn executes functionToExecute and unsubscribes itself. I have:
onDOMReady : function(functionToExecute){
subscribe(this.attachDOMHandler(functionToExecute));
},
attachDOMHandler : function(functionToExecute) {
unsubscribe(this.attachDOMHandler(functionToExecute));
functionToExecute();
}
Everything works fine when I have the following:
onDOMReady : function(functionToExecute){
subscribe(functionToExecute, this, true);
},
However I need the function to unsubscribe after it is executed. My plan was to use the attachDOMHandler function which executes the function and has the unsubscribe behaviour.
When I run the former I get a "Uncaught RangeError: Maximum call stack size exceeded" in the Chrome console. It looks like it gets stuck in an infinite loop which makes sense since I keep running attachDOMHandler in the unsubscribe which gets caught in a loop.
I basically need to subscribe a function that I specify by parameter passing. When the function is executed it unsubscribes.
Hope I have explained this ok.
You don’t want to call the function directly: