jQuery - show loading image only if ajax call takes more than one second?

前端 未结 2 388
终归单人心
终归单人心 2020-12-12 19:43

Is it possible to show the \"Loading..\" animation only if the ajax call takes more than say, one second? Some of my ajax calls are pretty fast but I still see the loading i

2条回答
  •  抹茶落季
    2020-12-12 20:27

    You could use a setTimeout().

    var loadingTimeout;
    $('#loading').hide()
    .ajaxStart(function() {
        var element = $(this);
        loadingTimeout = setTimeout(function() {
           element.show();
        }, 1e3);
    })
    .ajaxStop(function() {
        clearTimeout(loadingTimeout);
        $(this).hide();
    });
    

提交回复
热议问题