delay the showing of a ajax loading gif using jQuery

后端 未结 4 598
后悔当初
后悔当初 2021-02-06 01:11

What is the best way to put a delay on the showing of a ajax-loader gif. When I click a button the loader gif shows and hides even if the time taken is a few hundred milli-secon

4条回答
  •  隐瞒了意图╮
    2021-02-06 01:37

    You could also do it like this.

    var loadingTimer;
    
    $(document).ready(function () {
    
        $("#loader").ajaxStart(function () {
            loadingTimer = setTimeout(function () {
                $("#loader").show();
            }, 200);
        });
    
        $("#loader").ajaxStop(function () {
            clearTimeout(loadingTimer);
            $(this).hide();
        });
    
    }
    

提交回复
热议问题