简体   繁体   English

侦听要调用 JavaScript 的函数

[英]Listen for a function to be called JavaScript

I have the following functions that is called every 2 seconds to load some data.我有以下函数每 2 秒调用一次以加载一些数据。 It registers the function [ do ] to do the stuff with the response.它注册了函数 [ do ] 来处理响应。 (the example is simplified). (示例已简化)。

function doRequest (){
    $.ajax({ url: 'www.google.com.pe', success: function (response) {do(response)} });
}

function do (text){
    var i = setInterval(doRequest, 2000);
}

I wonder if there is any way that I can create a function that is called every time the [ do ] function is called with out needing to add a call to the listener inside the do function.我想知道是否有任何方法可以创建一个每次调用 [ do ] 函数时调用的函数,而无需在 do 函数内添加对侦听器的调用。 If there is any better way to do it with jQuery, like a plugin I'd appreciate the help.如果使用 jQuery 有更好的方法,比如插件,我将不胜感激。

[Edit] The idea is not whether it works or not. [编辑] 这个想法不在于它是否有效。 My question was about if I can add a custom listener to the "do" function which was already implemented.我的问题是我是否可以向已经实现的“do”函数添加一个自定义侦听器。 Something like addActionListener("do", "after", doSomeThingElse) ,sSo I could do some thing else just after the do function has finished.addActionListener("do", "after", doSomeThingElse)这样的东西,所以我可以在 do 函数完成后做一些其他的事情。

First, your simplified version won't work, because you'd need to pass the do function instead of calling it.首先,您的简化版本将不起作用,因为您需要传递do函数而不是调用它。

function doRequest (){
    $.ajax({ url: 'www.google.com.pe', success: _do });
}

But it sounds like you're asking how to run some other code every time do is invoked.但听起来您每次调用do时都在问如何运行其他代码。

If do is only invoked inside the doRequest() function, then just add your other code to an anonymous function that invokes do at the right time.如果do仅在doRequest()函数内部调用,则只需将您的其他代码添加到在正确时间调用do的匿名函数。

function doRequest (){
    $.ajax({ url: 'www.google.com.pe', success: function(response) {
          // Run your other code
          //   or invoke another function.
          _do(response);
    } });
}

If you want it to be more generalized, you can create a function decorator that returns a function which invokes do after some other code.如果你想让它更通用,你可以创建一个函数装饰器,它返回一个在其他代码之后调用do的函数。

function doFactory(fn) {
    return function() {
        fn.apply(this, arguments);
        _do.apply(this, arguments);
    }
}

then make functions like this:然后制作这样的功能:

var doFoo = doFactory(function() {
    console.log("foo");
});

If your requirement is more specific of a pre-processing of response , you could rework it like this:如果您的要求对response的预处理更具体,您可以像这样对其进行修改:

function doFactory(fn) {
    return function(response) {
        _do.call(this, fn.call(this, response));
    }
}

Then have the fn manipulate and return response .然后让fn操纵并返回response

var doFoo = doFactory(function(response) {
    return response + "foo";
});

If you want to keep existing code as it is, you could wrap do() in another function which in turn calls do() and your new function (say do_this_as_well() ).如果您想保持现有代码不变,您可以将do()包装在另一个函数中,该函数又调用do()和您的新函数(例如do_this_as_well() )。

See the example below (I renamed do() to do_this() to avoid confusion around the reserved keyword do ).请参阅下面的示例(我将do()重命名为do_this()以避免混淆保留关键字do )。 This works because global functions are nothing but variables with function objects in them.这是有效的,因为全局函数只不过是其中包含函数对象的变量。 These variables can be overwritten, in this case with a new function that calls the old one:这些变量可以被覆盖,在这种情况下,使用一个调用旧函数的新函数:

function do_this(response) { ... }

(function()
{
    var previous=do_this;
    do_this=function(response) { previous(response); do_this_as_well(); }
})();

Replace代替

success: do(response)

with

success: function(response) { do(response); do_this_as_well(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM