简体   繁体   中英

Timing Ajax Calls with Jquery Animations

I'm using the following script to load posts for each user's page.

$.post("postupdate.php", function(data){
var postoutput = $("#postoutput", data);
$("#postcontainer").load("postupdate.php?user=<? echo $profileuser_id; ?> #postoutput");
$( '#postcontainer' ).fadeIn();
}, "html"); 

Everything works fine except the fadeIn function. My load times vary, so sometimes the postcontainer div will fade in before it has been filled with posts from the Ajax Call. How can I control when the fadeIn function goes off so I can make sure it always finishes loading the content before showing the div? It looks really bad right now when they just appear out of thin digital air sometimes.

-Thanks

Use a callback for .load() and do the fade in there.

$("#postcontainer").load("postupdate.php?user=<? echo $profileuser_id; ?> #postoutput", function(){
    $( '#postcontainer' ).fadeIn();
});

From the documentation:

$("#postcontainer").load("postupdate.php?user=<? echo $profileuser_id; ?> #postoutput", function() {
    $('#postcontainer').fadeIn();
});

.load() has a callback function, either after the data to be sent, or the url. This function executes when the loading is complete.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM