Question
Is it possible to know if the function was fired by a user event or an async event (like callback) without having the original event parameter?
Background
I'm trying to determine the event source in a deeper function call which doesn't know who was the original event trigger.
I have to know that in order to call a popup or redirection login system. But this function is called from many places, so I can't pass the event parameter in all callers.
Important: I'm not able to pass parameters to the final function. b('timer')
is not allowed.
e.g:
<a onclick="b()" >call</a>
<script>
function a(){
b();
}
function b(){
final();
}
function final(){
//Is there something like this caller.event.source ?
console.log(this.caller.event.source)
}
setTimeout(a,1000);
In that example, I'm trying to get source == 'timer'
or 'onclick'
, or any other information to determine which is the event origin.
Update
Based on basilikun approach I've implemented this solution:
function final(){
var callerFunction = arguments.callee.caller,
evtArg = callerFunction.arguments[0];
while(callerFunction.caller){
callerFunction = callerFunction.caller;
if (callerFunction.arguments[0]) {
evtArg = callerFunction.arguments[0];
}
}
console.log(evtArg&&evtArg.type?'event fired by user':'event async');
}
This is the finddle
Any other approach?