0

I don't know when a function was called. I'm trying to change the text of a p element with an id of downloads, that I know how to do. But how do you figure out when a function was called? The function is named move. Something like this? I just need when a function was called. Maybe an event listener?

    if (move.wasCalled) {
        // Some code
    }
Community
  • 1
  • 1
Brandon
  • 3
  • 6
  • If you did a google search, you would've saved yourself time rather than writing this question https://stackoverflow.com/questions/3332265/how-to-detect-a-function-was-called-with-javascript – Abdul Ahmad Jul 26 '18 at 16:03
  • 2
    Do you want to execute some code _every time_ the function called - if yes, insert the code inside the function - or do you want to know if a function called? – Chayim Friedman Jul 26 '18 at 16:03
  • 2
    Do you want 'when' or 'if' function was called? 'when' ---> new Date().getTime() then update a global scope var with this time whereas 'if' ---> use a Boolean flag, set global scope var to true when func called – CSSBurner Jul 26 '18 at 16:07
  • I did do a google search. – Brandon Jul 26 '18 at 16:08
  • I want to know if a function was called – Brandon Jul 26 '18 at 16:11
  • I voted to reopen this as the dupe was not exact, although the title suggested that – Jonas Wilms Jul 26 '18 at 16:16
  • Possible duplicate of [how to detect a function was called with javascript](https://stackoverflow.com/questions/3332265/how-to-detect-a-function-was-called-with-javascript) – Reza Saadati Jul 26 '18 at 16:27
  • @RezaSaadati No, that quastion doesn't have the answer i'm looking for. – Brandon Jul 26 '18 at 16:32

2 Answers2

0

Seems as if you want to add an eventlistener for when move is called:

 move = (function(old) {
     var handlers = [];
     function move() {
        old.apply(this, arguments);
        handlers.forEach(function(handler) { handler(); });
     }
     move.wasCalled = function(handler) { handlers.push(handler) };
     return move;
   };
})(move);

So you can use it as:

 move.wasCalled(function() {
   //...
 });

 move();

If you however want to determine synchronously if the function was added already, just set a property when move was called, it can be rewritten as:

move = (old => (...args) => {
  move.wasCalled = true;
  old(...args);
})(move);

Or for older environments:

move = (function(old) { 
  return function() {
    move.wasCalled = true;
    old.apply(null, arguments);
  };
})(move);

So now

 if(move.wasCalled) { /*...*/ }

actually works

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Whats args? And what should be before args? Sorry, I'm kind of a noob at js – Brandon Jul 26 '18 at 16:10
  • @sadieKoe if you add this code block after the declaration of `function move() { }` and before you call it the first time, the `move.wasCalled` will be set. Now `(...args)` takes all arguments that are passed to the function that replaces the original function and will then call the original function with that, so `move(1,2,3)` will still work – Jonas Wilms Jul 26 '18 at 16:12
  • When i put it in the code, it gives an error, "Unexpected token >" – Brandon Jul 26 '18 at 16:16
  • I just did! Sorry! – Brandon Jul 26 '18 at 16:18
  • @sadieKoe no worries, its just that a 2 year old version is likely to be vulnerable to a lot of attacks, so its not just that newer code won"t work its also a security thing – Jonas Wilms Jul 26 '18 at 16:20
  • Ok. Thanks for telling me that. – Brandon Jul 26 '18 at 16:21
  • Sorry, but what is the ... for? Also, I got an error, "Unexpected token >" – Brandon Jul 26 '18 at 16:23
  • @sadieKoe okay then forget about your browser, its KhanAcademys fault. They didn't expect that you would write new code :). I added a version that should work there – Jonas Wilms Jul 26 '18 at 16:27
  • Thank you so much! But for some reason, its not doing anything. I added a `console.log("Hi");`, but nothing happened – Brandon Jul 26 '18 at 16:28
  • You still here? – Brandon Jul 26 '18 at 16:37
  • I did this. `if (move.isCalled) { console.log("Hi"); }`, but nothing happened – Brandon Jul 26 '18 at 18:55
  • Hello! You here? – Brandon Jul 26 '18 at 19:37
  • I think you are actually looking for some kind of event listener, but thats hard to tell. I guess its the best if you just continue your course without this, and get back here at a later point, then we can continue this :) – Jonas Wilms Jul 26 '18 at 20:06
0

You can add console.log("move function was called."); inside the if statement move.wasCalled like this:

if (move.wasCalled) {
    // Some code
    console.log("move function was called.");
}

So when the if statement is executed it will be logged in the console e.g.

Check in the Google Chrome dev tools console tab.