每次运行特定功能时执行Jquery [英] Execute Jquery every time a specific function runs

查看:101
本文介绍了每次运行特定功能时执行Jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次特定函数运行时,是否可以执行某些操作,而不需要知道该函数的名称?

这将会是类似于绑定

  var clicked = 0; 
('#foo')。bind('click',function(){
clicked + = 1;
alert(clicked);
});

因此,每次使用ID foo 被点击后,它会向变量点击添加1,这样我就知道它被点击了多少次。如果它是正确的语法,我想要做的就是以下的等价:

  var fired = 0; 
('my_function')。bind('run',function(){
fired + = 1;
alert(fired);
});

我不在乎在任何特定的情况下,您会一直能够找出关于这个函数的东西并使用它,我不想要工作,这就是我想要的答案:

如何在每次特定的时间执行某些操作函数运行,只是给出了函数的名称。如果这是不可能的,为什么不呢?

解决方案

试试这样:

  var temp = my_function,fired = 0; 
my_function = function(){
fired ++;
temp.apply(this,arguments);
}


Is it possible to execute something every time a specific function runs without any knowledge about that function besides its name?

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?

解决方案

Try something like this:

var temp = my_function, fired = 0;
my_function = function() {
    fired++;
    temp.apply(this,arguments);
}

这篇关于每次运行特定功能时执行Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆