Set a delay in ajax call

后端 未结 4 1690
借酒劲吻你
借酒劲吻你 2021-01-06 02:12

I am trying to add a small delay (2 sec) between the loader icon and the success with the data as html.

What I have tried to use is the setTimeout and put in a delay

相关标签:
4条回答
  • 2021-01-06 02:43

    here is something i found when i wanted to do the same :

    function doStuff()
    {
      //do some things
      setTimeout(continueExecution, 10000) //wait ten seconds before continuing
    }
    
    function continueExecution()
    {
       //finish doing things after the pause
    }
    

    hope it will help ya

    0 讨论(0)
  • 2021-01-06 02:48

    setTimeout should be used inside success function.

    $(function() {
      var delay = 2000;
      var res = {
        loader: $("<div />", {
          class: "loader"
        })
      };
      $('#search').on('click', function() {
        $.ajax({
          type: 'GET',
          url: "@Url.Action("Find", "Hotel")",
          datatype: "html",
          beforeSend: function() {
            $("#group-panel-ajax").append(res.loader);
          },
          success: function(data) {
            setTimeout(function() {
              delaySuccess(data);
            }, delay);
          }
        });
        return false;
      });
    });
    
    function delaySuccess(data) {
      $("#group-panel-ajax").find(res.loader).remove();
      $('#group-panel-ajax').html($(data).find("#group-panel-ajax"));
    }
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2021-01-06 02:48

    $(function() {
      var delay = 2000;
      var res = {
        loader: $("<div />", {
          class: "loader"
        })
      };
      $('#search').on('click', function() {
        $.ajax({
          type: 'GET',
          url: "@Url.Action("Find", "Hotel")",
          datatype: "html",
          beforeSend: function() {
            $("#group-panel-ajax").append(res.loader);
          },
          success: function(data) {
            setTimeout(function() {
              delaySuccess(data);
            }, delay);
          }
        });
        return false;
      });
    });
    
    function delaySuccess(data) {
      $("#group-panel-ajax").find(res.loader).remove();
      $('#group-panel-ajax').html($(data).find("#group-panel-ajax"));
    }
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2021-01-06 02:55

    Use setTimeout() like this:

    <script type="text/javascript">
    
    $(function () {
    
        var delay = 2000;
    
        var res = {
            loader: $("<div />", { class: "loader" })
        };
    
        $('#search').on('click', function () {
            $.ajax({
                type: 'GET',
                url: "@Url.Action("Find", "Hotel")",
                datatype: "html",
                beforeSend: function () {
                    $("#group-panel-ajax").append(res.loader);
    
                },
    
                success: function (data) {
                    setTimeout(function(){
                         $("#group-panel-ajax").find(res.loader).remove();
                         $('#group-panel-ajax').html($(data).find("#group-panel-ajax"));
                    }, delay);
    
                }
            });
            return false;
        });
    });
    
    </script>
    
    0 讨论(0)
提交回复
热议问题