Vahe
Vahe

Reputation: 23

JS: how to listen for function execution?

I need to implement a listener which will perform a certain action if FunctionA is called on the page at any time (during or after page load).

Or in other words I need to listen for an execution of FunctionA.

How to achieve this?

Thank you.

Upvotes: 0

Views: 3183

Answers (3)

Bálint
Bálint

Reputation: 4039

You can make a self-wrapping function the following way.

var _ = functionA; // This variable needs to be always available to functionA
functionA = function(arg1, arg2) {
    _(arg1, arg2);
    alert("functionA was caled!");
}

Change the alert part to you event

This way everytime anyone calls this function you get notified.

Upvotes: 1

eltonkamami
eltonkamami

Reputation: 5190

if you are sure function A has synchronous code you can wrap it in a function of yours and track its execution that way

function _A(){
  A.call(undefined, ...arguments);
  // A has finished execution
}

if the code is asynchronous then the issue is more complicated

Upvotes: 0

Cristian Batista
Cristian Batista

Reputation: 281

You can call a Function at the end/start of your FunctionA:

   function FunctionA(){

       /* Your method implementation here */

       FunctionB();
   }

Upvotes: 0

Related Questions