简体   繁体   English

在另一个函数内部运行函数,但仅在ajax完成之后

[英]Run function inside of another function, but only after ajax has finished

I have a function that is run inside of another function, but sometimes it seems my ajax call to the server isn't finished running before it calls the function. 我有一个在另一个函数内部运行的函数,但有时似乎我对服务器的ajax调用在调用该函数之前并未完成运行。

    function getPlans(row, user, id, type) {
      $.get( "/plans/" + user + "/" + id + "/" + type + "", function(data) {
        $("#permissions_" + row + "_plan_type").empty();
        $(data).each(function(i, e) {
          $("#permissions_" + row + "_plan_type").append("<option>" + e + "</option>");
        })
      }).done(function() {
        getPermissions(row, user, type);
      })
    }

As a quick solution you can make an .ajax call with async: false 作为快速解决方案,您可以使用async: false进行.ajax调用async: false

 function getPlans(row, user, id, type) {
  $.ajax({
    url:"/plans/" + user + "/" + id + "/" + type + "",
    type: 'GET',
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        $("#permissions_" + row + "_plan_type").empty();
        $(data).each(function(i, e) {
          $("#permissions_" + row + "_plan_type").append("<option>" + e + "</option>");
        })
      },
      async:false // make synchronous
  });

  getPermissions(row, user, type);
}

Solution 2: You can use .when from jQuery. 解决方案2:您可以在jQuery中使用.when

function getPlans(row, user, id, type) {
    var dfd = $.Deferred();
    $.when( $.get( "/plans/" + user + "/" + id + "/" + type + ""))
        .done(function(data) {
            $("#permissions_" + row + "_plan_type").empty();
            $(data).each(function(i, e) {
              $("#permissions_" + row + "_plan_type").append("<option>" + e + "</option>");
            })
        });

    getPermissions(row, user, type);
}

Try calling getPermissions() from success callback. 尝试从成功回调中调用getPermissions()

function getPlans(row, user, id, type) {
    $.get( "/plans/" + user + "/" + id + "/" + type + "", function(data) {
        $("#permissions_" + row + "_plan_type").empty();
        $(data).each(function(i, e) {
          $("#permissions_" + row + "_plan_type").append("<option>" + e + "</option>");
        });
        getPermissions(row, user, type);
    });
 }

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

相关问题 如何仅在页面加载完成后以及调用另一个函数之后才调用函数 - How to call a function only after page has finished loading and after another function has been called 仅在另一个函数完成时运行一个函数 - Run one function only when another is finished Casperjs Fill Method使next函数仅在previouse函数完成后运行 - Casperjs Fill Method make next function only run after the previouse function has finished 仅在上一个函数完成后才允许Javascript函数运行 - Only allowing a Javascript function to run when the previous function has finished 仅当另一个函数在纯 JavaScript 中完成时才触发函数? - Trigger function only if another function has finished in pure JavaScript? 只有在另一个脚本完成后才运行脚本的方法是什么? - What are ways to run a script only after another script has finished? 在循环内使用异步函数并仅在循环完成后调用回调 - Using async function inside a loop and calling a callback only after the loop has finished 仅在第一个 function 完全完成后运行第二个 function - Run a second function only after the first function is completely finished 在ajax调用完成在Laravel中加载图像后触发函数 - Triggering a function after ajax call has finished loading images in Laravel 如何仅在第一个完成后才触发 function(JavaScript) - How to fire a function only after the first one has finished in (JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM