![Execute Jquery every time a specific function runs [duplicate]](https://www.xszz.org/skin/wt/rpic/t5.jpg)
Question:
This question already has an answer here:
<ul><li> <a href="/questions/17202346/do-something-when-function-executes-jquery" dir="ltr" rel="nofollow">do something when function executes Jquery</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul><strong>Is it possible to execute something every time a specific function runs without any knowledge about that function besides its name?</strong>
This would be similar to bind
var clicked = 0;
$('#foo').bind('click',function(){
clicked += 1;
alert(clicked);
});
So there, every time something with the ID foo
is clicked, it will add 1 to the variable clicked
so that I know how many times it has been clicked. What I want to do would be the equivalent of the following if it were correct syntax:
var fired = 0;
$('my_function').bind('run',function(){
fired += 1;
alert(fired);
});
I don't care if in any given situation you would be in, you would always be able to figure something out about the function and use that, I don't want work arounds, this is what I want for an answer:
How I can execute something everytime a specific function runs, just given the name of the function. If that is not possible, why not?
Answer1:Try something like this:
var temp = my_function, fired = 0;
my_function = function() {
fired++;
temp.apply(this,arguments);
}
Answer2:I think something like this may be the closest you can come:
function adjustFunctionToCount(f){
var count = 0;
function newF(){
count++;
f.apply(this, arguments);
}
newF.getCount = function(){ return count; };
return newF;
}
And so if you have
function handler(val){
console.log('called with val ' + val);
}
You could do
handler = adjustFunctionToCount(handler);
handler('a');
handler('b');
console.log(handler.getCount());
<a href="http://jsfiddle.net/7sM3L/" rel="nofollow">FIDDLE</a>
And needless to say you could create your function inline
var handler = adjustFunctionToCount(function(val){ console.log('called with val ' + val); });
handler('a');
handler('b');
console.log(handler.getCount());
<a href="http://jsfiddle.net/7sM3L/2/" rel="nofollow">UPDATED FIDDLE</a>
Answer3:I'm pretty sure that's impossible in the general case.
Remember, functions are objects, really, and the name of a function is just a variable. Functions can exist without being assigned to a named variable, the variables can be out of your scope, or reassigned/swapped around. In any case, I know of no API that lets you hook onto a JS function call.
This may be of interest: <a href="https://stackoverflow.com/questions/3406467/can-i-intercept-a-function-called-directly" rel="nofollow">Can I intercept a function called directly?</a>
Answer4:This is where event driven programming comes in - and jQuery makes it really easy to do.
var myFunction = function() {
//...
//...
//...
$(document).trigger('someEvent');
}
$(document).on('someEvent',function() {
//the function you would like to run every time myFunction is called
});
Answer5:Try this:
var count = (function(){
var c = 0;
return function(){
alert(c++);
};
})();
$('#foo').click(count);
OR
$('#foo').bind('click', count);
When an Anonymous Function or a Variable that represents a Function is passed it is the same thing. You could make your own code that executes a Function like this:
function executeFun(func){
return func();
}
executeFun(count)
executeFun(function(){
/*everything happens in here. The Anonymous Function will be called
automatically because of the parameter next to the variable func above */
})
Although, that example is impractical it shows you what happens internally. Also, I solved your potential global scope variable problem with a Closure. For more on Closures visit <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures</a> .