do something when function executes Jquery

后端 未结 2 1517
孤城傲影
孤城傲影 2020-12-22 11:54

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         


        
相关标签:
2条回答
  • 2020-12-22 12:00

    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++; 
    };
    
    0 讨论(0)
  • 2020-12-22 12:17

    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
    
    0 讨论(0)
提交回复
热议问题