Keith L.
Keith L.

Reputation: 2124

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

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.

Upvotes: 1

Views: 4644

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337560

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);
    }
}

Upvotes: 1

erimerturk
erimerturk

Reputation: 4288

did you try this ?

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

Upvotes: 2

Related Questions