以正确的顺序执行jQuery脚本

Luk*_*oss 0 javascript jquery

我有一个chrome扩展,包含一个html页面和一些.js脚本.

在我的页面上,我有一个按钮,

HTML

<script src='api.js'></script>
<script src="jquery-git.js" type="text/javascript"></script>
<button name="Log" type="submit" id="Log-submit">Save Result</button>
Run Code Online (Sandbox Code Playgroud)

api.js

//  Add document to Elastic Search (Log)
$(document).ready(function() {
  $('#Log-submit').on('click', function(e) {
    e.preventDefault();
    var btn = $(e.target);
        btn.attr("disabled", "disabled"); // disable button
    $.ajax({
      data: JSON.stringify({ name: $( "div.Name" ).text(), xentime: entime, incident: "INC-"+ $("#incID").val(), team: $( "div.Team" ).text(), response: $("#option_id option:selected").text(), timestamp: $.now(), xuser: $("div.xuser").text(), xlocation: $("div.xloc").text()}),  //use variable value/text in JSON POST request
      dataType: 'json',
      type: 'POST',
      contentType: "application/json; charset=utf-8",
      url: 'http://myapi.com:9200/escalations/escalation/'
    });
  });
});


// Close Window
 $(document).ready(function() {
  $("#Log-submit").click(function() {
   window.close();
  });
});
Run Code Online (Sandbox Code Playgroud)

单击时,它会对我的elasticsearch索引进行API调用,并填充一些JSON.

然后我想让窗户关闭.

在小步舞时我运行时没有' Close window'我的数据填充但窗口保持打开状态.

如果我运行' Close Window'我的数据不会填充但我的窗口关闭.

我怎样才能让它以正确的顺序运行?

Bar*_*mar 5

关闭窗口正在取消AJAX请求.在AJAX请求的成功函数中执行此操作.

$(document).ready(function() {
  $('#Log-submit').on('click', function(e) {
    e.preventDefault();
    var btn = $(e.target);
        btn.attr("disabled", "disabled"); // disable button
    $.ajax({
      data: JSON.stringify({ name: $( "div.Name" ).text(), xentime: entime, incident: "INC-"+ $("#incID").val(), team: $( "div.Team" ).text(), response: $("#option_id option:selected").text(), timestamp: $.now(), xuser: $("div.xuser").text(), xlocation: $("div.xloc").text()}),  //use variable value/text in JSON POST request
      dataType: 'json',
      type: 'POST',
      contentType: "application/json; charset=utf-8",
      url: 'http://myapi.com:9200/escalations/escalation/',
      success: function() {
        window.close();
      }
    });
  });
});
Run Code Online (Sandbox Code Playgroud)