简体   繁体   English

onclick 内部 Ajax 调用

[英]onclick inside Ajax call

I have an ajax call that on success create an element.我有一个 ajax 调用,成功创建一个元素。 When the element is clicked, a spinning gif needs to be displayed while another action is being done (creating jquery datatables).单击该元素时,需要显示一个旋转的 gif,同时执行另一个操作(创建 jquery 数据表)。 Then the gif should disappear.然后gif应该消失。 In my case everything works except for the fact that the gif is loading after the jquery datatables is created and therefore only shown very briefly.在我的情况下,除了在创建 jquery 数据表之后加载 gif 并且因此仅显示非常简短的事实之外,一切正常。 I want it to start as soon as I click the element, but this is not the case.我希望它在单击元素后立即启动,但事实并非如此。

ajax{
element.create();
element.click(){
      startLoadingGif();

      drawJqueryDataTables();

      stopLoadingGif();
}
}

I would appreciate any help.我将不胜感激任何帮助。

What you need to do is this.你需要做的是这个。 startLoadingGif() before the request is made and stopLoadingGif();在发出请求之前 startLoadingGif( startLoadingGif()stopLoadingGif(); after the completion (success or failure) of the request:请求完成(成功或失败)后:

   startLoadingGif();
     $.ajax({
      ...
      complete: function (data) {
          stopLoadingGif()
        }
     });

UPDATE:更新:

$.ajax({
      ...
      complete: function (data) {
           element.create();
           element.click()
           startLoadingGif();
           drawJqueryDataTables(); //Or you can make this a promise if it takes a longer time and after that is resolved you can stop the spinner. Or,
           window.setTimeout(()=>{
             stopLoadingGif();
           }, 2000)

     });

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

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