0

So I have a function that is recursive for inverting colors. Here is the code:

function invert(id,what){
    var color = $(id).css(what);
    var matchColors = /rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/;
    var match = matchColors.exec(color);
    var r = (255 - match[1]).toString() + ",";
    var g = (255 - match[2]).toString() + ",";
    var b = (255 - match[3]).toString();
    answer = 'rgb(' + r + g + b + ')' ;
    $(id).css(what,answer);
};

So essentially I have a function that can be called in many instances (clicks of specific ids, hover on specific classes, etc.) and I do not know them all. But I need to know every single time this function gets called. How can I have an outside line of code that sets a variable equal to the amount of times the function has been called?

3
  • 1
    If you want to count the number of times the function called itself, you need to modify its source code. Commented Jun 19, 2013 at 22:20
  • there is no way to count, outside of the function, how many times a function fired if it is recursive? I find that hard to believe Commented Jun 19, 2013 at 22:21
  • @RyanSaxe it is hard to believe you cannot override an function Commented Jun 19, 2013 at 22:33

2 Answers 2

6

Wrap your function.

var wrapped = (function wrapper(present) {
    function wrapping() {
        ++wrapping.count; // increment invocation count
        return present.apply(this, arguments);
    }
    wrapping.count = 0; // counter, avaliable from outside too
    return wrapping;
}(invert));

If you need to call it invert too, re-assign invert after.

invert = wrapped;
invert.count; // 0
invert();
invert.count; // 1
invert();
invert.count; // 2
Sign up to request clarification or add additional context in comments.

3 Comments

please re-read the question...I have made an edit so that it may be possible completely outside the original function as the function is no longer recursive
@RyanSaxe are you saying that the function invert is declared in a scope you don't have access to?
This works, although I am going to post a question to see if it is possible to detect becuase I am curious anyways
0

I am not sure what your exact scenario is, but maybe you could override the function with a wrapper:

var invertOriginal = invert;
var counter = 0;

var invert = function(id, what, max) {
  invertOriginal(id, what, max); 

  // do counter stuff here, e.g.
  counter++; 
};

3 Comments

can't change original function
this doesn't change the original function. It simply "renames" the function and calls the renamed version. You can use this code without modifying the original source
please re-read the question...I have made an edit so that it may be possible completely outside the original function as the function is no longer recursive

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.