jQuery ajax loading - display the loading image for a min. of 1 second

对着背影说爱祢 提交于 2019-12-11 13:56:01

问题


i have a jquery ajax request. On page load a loading-image is displayed. this is working well, but i want to display the loading gif for a minimum of 1 second.

I've already tried this: Ajax loader image: How to set a minimum duration during which the loader is shown? , but it's not working.

My code is:

<div id="ajaxcontent"><img src="loader.gif" id="ajaxloading" /></div>

and

$.ajax({
// some code for the request
success: function () {
             $('#ajaxcontent').text(myResultText);
        }

I've also tried to add .delay(1000) before the text is shown, but this also does not work.


回答1:


did you try this ?

   setTimeout(function() { $('#ajaxcontent').text(myResultText);},
               1000);



回答2:


The delay() method in jQuery relates to animations, so it won't delay the updating of any properties of an element. Instead, try using the setTimeout() function, which runs the code you specify after waiting for a set number of miliseconds:

$.ajax({
    // some code for the request
    success: function () {
        setTimeout(function() {
            $('#ajaxcontent').text(myResultText);
        }, 1000);
    }
}


来源:https://stackoverflow.com/questions/8307924/jquery-ajax-loading-display-the-loading-image-for-a-min-of-1-second

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!