简体   繁体   English

如何在另一个完成后运行功能

[英]how to run function after another one completes

I have problem to run function after another completes.我在另一个完成后运行功能时遇到问题。 I try callback like in this post but this dont work Execute jquery function after another function completes so maybe i do something wrong or my case is more complicated.我在这篇文章中尝试回调,但这不起作用在另一个函数完成后执行 jquery 函数,所以也许我做错了什么或者我的情况更复杂。

So I have function that run when I submit the form所以我有提交表单时运行的功能

$("#search-form").submit(ajaxSubmit(addSearchfuntion)); // addSearchfuntion is callback

function ajaxSubmit(callback) {
    var $form = $(this);
    var settings = {
        data: $(this).serialize(),
        url: $(this).attr("action"),
        type: $(this).attr("method")
    };

    $.ajax(settings).done(function(result) {
        var $targetElement = $($form.data("ajax-target"));
        var $newContent = $(result);
        $($targetElement).replaceWith($newContent);
        $newContent.effect("slide");
    });

    callback();

    return false;
};

After this when I get new form to my page i want to run another function that will handle this new form.在此之后,当我将新表单添加到我的页面时,我想运行另一个函数来处理这个新表单。

 function addSearchfuntion(){
     $('.Amount').change(updateQuantity);
 }

So how to solve this case?那么如何解决这个案子呢?

You need to use anonymous function to bind the submit event handler.您需要使用匿名函数来绑定submit事件处理程序。 As of now you are executing the ajaxSubmit() method and binding its return value ie false as event handler.到目前为止,您正在执行ajaxSubmit()方法并绑定其返回值,即false作为事件处理程序。

$("#search-form").submit(function(){
    ajaxSubmit.bind(this)(addSearchfuntion);
});

And, invoke the callback method in the done() callback method of $.ajax()而且,调用callback的方法done()的回调方法$.ajax()

$.ajax(settings).done(function(result) {
    // Your existing code
    ....
    ....
    // call the callback
    callback();
});

You where pretty close, you need to place the callback call inside the method that is asynchronous您非常接近,您需要将回调调用放在异步方法中

function ajaxSubmit(callback) {
    // this will be executed linear
    var $form = $(this);
    var settings = {
        data: $(this).serialize(),
        url: $(this).attr("action"),
        type: $(this).attr("method")
    };

    // this will be executed async
    $.ajax(settings).done(function(result) {

        // anything in here must be handled async, hence put your callback here

        var $targetElement = $($form.data("ajax-target"));
        var $newContent = $(result);
        $($targetElement).replaceWith($newContent);
        $newContent.effect("slide");

        // call the callback
        callback();
    });

    // this will be executed linear and most likely before the async method
    return false;
};

Simply return function in ajaxSubmit :只需在ajaxSubmit返回函数:

function ajaxSubmit(callback) {
  return function(){
  var $form = $(this);
  var settings = {
    data: $(this).serialize(),
    url: $(this).attr("action"),
    type: $(this).attr("method")
  };

  $.ajax(settings).done(function(result) {
    var $targetElement = $($form.data("ajax-target"));
    var $newContent = $(result);
    $($targetElement).replaceWith($newContent);
    $newContent.effect("slide");
  });

  callback();

  return false;
}
};

Place your callback execution in your ajax(settings).done section.将您的callback执行放在您的ajax(settings).done部分。 It'd look like this:它看起来像这样:

$.ajax(settings).done(function(result) {
    var $targetElement = $($form.data("ajax-target"));
    var $newContent = $(result);
    $($targetElement).replaceWith($newContent);
    $newContent.effect("slide");

    if (typeof callback === "function") {
        callback();
    }
});

You should also check to make sure callback exists before calling it, which is the thinking behind the if (typeof callback)... check.您还应该在调用之前检查以确保callback存在,这是if (typeof callback)... check 背后的想法。 See this answer for more information.有关更多信息,请参阅此答案

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

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