函数执行完成后如何调用函数? - JavaScript

发布于 2024-12-12 02:18:52 字数 200 浏览 0 评论 0原文

我正在使用一个用户控件,该控件具有一组在执行操作时调用的 JavaScript 函数。该用户控件在应用程序的很多地方使用。

当内置 JS 函数之一完成执行时,我需要在页面上触发自定义 JS 函数。

有没有办法让我附加一个函数,当另一个函数完成执行时要触发该函数?我不想更新内置的 JS 函数来调用此页面的 JS 函数。

希望这是有道理的。

I am working with a user control that has set of javascript functions that are called when an action is performed. This user control is used in a lot of places in the application.

When one of the inbuilt JS function completes execution, I need to fire a custom JS function on my page.

Is there a way for me to attach a function to be fired when another function completes execution? I don't want to update the inbuilt JS function to call this page JS function.

Hope this makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

多情癖 2024-12-19 02:18:53

最丑陋和最好的解决方案是对内置函数进行猴子修补。假设内置函数称为“thirdParty”:

// first, store a ref to the original
var copyOfThirdParty = thirdParty;
// then, redefine it
var thirdParty = function() {
    // call the original first (passing any necessary args on through)
    copyOfThirdParty.apply(this, arguments);
    // then do whatever you want when it's done; 
    // custom code goes here
    customFunction();
};

我们实际上创建了内置函数的修改版本,而没有触及原始版本。

The ugliest and best solution is to monkey-patch the built-in function. Assume the built-in function is called "thirdParty":

// first, store a ref to the original
var copyOfThirdParty = thirdParty;
// then, redefine it
var thirdParty = function() {
    // call the original first (passing any necessary args on through)
    copyOfThirdParty.apply(this, arguments);
    // then do whatever you want when it's done; 
    // custom code goes here
    customFunction();
};

We've essentially created a modified version of the built-in function without ever touching the original version.

小猫一只 2024-12-19 02:18:53

由于 Javascript 是高度动态的,您可以修改原始函数而无需修改其源代码:

function connect_after(before, after){
    return function(){
        before.apply(this, arguments);
        after();
    };
}

var original_function = function(){ console.log(1); }
original_function = connect_after(original_function, function(){ console.log(2); })

Since Javascript is highly dynamic you can modify the original function without modifying its source code:

function connect_after(before, after){
    return function(){
        before.apply(this, arguments);
        after();
    };
}

var original_function = function(){ console.log(1); }
original_function = connect_after(original_function, function(){ console.log(2); })
伴我老 2024-12-19 02:18:52

您可以使用几种设计模式来实现此目的,具体取决于特定代码(您尚未共享的代码)以及可以更改和不能更改的内容:

添加回调:

function mainFunction(callbackWhenDone) {
    // do other stuff here
    callbackWhenDone();
}

选项 1:向某些现有代码 ,您可以使用以下方式调用它:

mainFunction(myFunction);

选项 2:包装以前的函数:

obj.oldMethod = obj.mainFunction;
obj.mainFunction = function() {
    this.oldMethod.apply(this, arguments);
    // call your stuff here after executing the old method
    myFunction();
}

因此,现在任何时候有人这样做:

obj.mainFunction();

它都会调用原始方法,然后调用您的函数。

There are a couple design patterns you could use for this depending upon the specific code (which you have not shared) and what you can and cannot change:

Option 1: Add a callback to some existing code:

function mainFunction(callbackWhenDone) {
    // do other stuff here
    callbackWhenDone();
}

So, you can call this with:

mainFunction(myFunction);

Option 2: Wrap previous function:

obj.oldMethod = obj.mainFunction;
obj.mainFunction = function() {
    this.oldMethod.apply(this, arguments);
    // call your stuff here after executing the old method
    myFunction();
}

So, now anytime someone does:

obj.mainFunction();

it will call the original method and then call your function.

如歌彻婉言 2024-12-19 02:18:52

您基本上是在尝试进行回调。由于您没有提及您正在谈论的函数(如代码中所示),因此最好的办法基本上是包装该函数(快速而肮脏)并使其与回调一起使用。

这样你就可以向它传递一个 Lambda(匿名函数)并在完成后执行你想要的任何操作。

更新以演示如何添加回调:

function my_function($a, $callback) {
   alert($a);
   $callback();
}

my_function('argument', function() { 
  alert('Completed');
});

You're basically trying to do callbacks. Since you're not mentioning what functions you're talking about (as in code), the best thing to do would be basically to wrap the function, -quick and dirty- and make it work with callbacks.

That way you can pass it a Lambda (Anonymous Function) and execute anything you want when it's done.

Updated to demonstrate how to add Callbacks:

function my_function($a, $callback) {
   alert($a);
   $callback();
}

my_function('argument', function() { 
  alert('Completed');
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文