简体   繁体   English

jQuery-函数完成后返回

[英]jQuery - Return once function finishes

I feel like this is a fairly simple thing, but I can't seem to figure it out. 我觉得这是一件相当简单的事情,但我似乎无法弄清楚。 I need a function to return it's value after an action has been completed. 我需要一个函数来在动作完成后返回它的值。

var msg = my_function();
alert(msg);

function my_function(){
  box = $("<div>");
  button = $("<span>submit</span>").click(function(){
    box.remove();
    return "Hello World!";
  });
  $("body").append(box);
} 

However when I do this, it returns msg as 'undefined' before the box is even appended to the body tag. 但是,当我这样做时,它甚至在框附加到body标签之前都将msg返回为“ undefined”。 How do I add a 'on complete' or something along those lines to this? 如何在此基础上添加“完成”或类似内容?

my_function won't wait until the user performs an action. my_function不会等到用户执行操作。 What you are passing to .click() is another function which will be attached as an event handler and executed when the event occurs. 您传递给.click()是另一个函数,它将附加为事件处理程序,并在事件发生时执行。 This is called asynchronous (and in this case also event-driven) behaviour. 这称为异步(在这种情况下,也是事件驱动)行为。

The trick to get the return value you are looking for is to provide your own callback to my_function which will called with that return value when it is available. 获得所需的返回值的技巧是为my_function提供自己的回调,该回调将在可用时使用该返回值进行调用。 Note that this callback will be executed any time someone clicks the button (which you have to add to the body, by the way, otherwise it wouldn't show up anywhere): 请注意,此回调将在有人单击按钮时执行(顺便说一句,您必须将其添加到正文中,否则它不会在任何地方显示):

var myCallback = function(msg) {
    alert(msg);
}

var msg = my_function(myCallback);

function my_function(callback) {
  var box = $("<div>");
  var button = $("<span>submit</span>").click(function(){
    box.remove();
    callback("Hello World!");
  });
  $("body").append(box).append(button);
} 

You put a return in the wrong place. 您将退货放在错误的位置。 A return statement inside a callback function will do nothing. 回调函数中的return语句不会执行任何操作。

var msg = my_function();
alert(msg);

function my_function(){
  box = $("<div>");
  button = $("<span>submit</span>").click(function(){
    box.remove();
    return "Hello World!"; // this return does not make sense
  });
  $("body").append(box);
  return "Hello World!"; // return here what you want

} }

I think this is what you want: 我认为这是您想要的:

my_function(function(msg) { alert(msg); });

function my_function(callbackFn){
  box = $("<div>");
  button = $("<span>submit</span>").click(function(){
    box.remove();
    callbackFn("Hello World!");
  });
  $("body").append(box);
} 

But you should put that span on the page somewhere. 但是您应该将该span放在页面上的某个位置。

Whatever you want to do, you need to put it into your 无论您想做什么,都需要将其放入您的

click(function() { ... }

. Explanation: This function will only return something after actually the "click" has taken place. 说明:该函数仅在实际发生“单击”之后才返回内容。

Since your "my_function()" would only return anything (which it does not) after the click, "msg" will always be undefined! 由于单击后您的“ my_function()”仅返回任何内容(不会返回任何内容),因此“ msg”将始终是未定义的! -> The "alert" does not wait until "msg" has been assigned ... ->“警报”不会等到分配了“ msg”后才开始...

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

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