Asynchronous functions fire in order of appearance but return in order of when they finish. It would be odd for those simple text
and removeClass
methods to return more slowly than whatever you're doing with your Server
object, but I suppose it's possible. If you need the first two lines to happen before the postService
, you might try jQuery's deferred.promise
. Here's a fiddle demonstrating the potential, and some code to inspect:
function firstThing (){
var dfd = new jQuery.Deferred();
$('#import').text("Importing...");
$('#import img').removeClass("hidden");
dfd.resolve();
}
$.when( firstThing() ).then(
function() {
Server.postService("tests", row_datas, function(data) {
// some stuff here
});
}
)
As a side note, the logic of your code is problematic in that by setting the text
of #import
, whatever img
had the hidden
class won't be there anymore, but that might be beside the point.
Update
Noticing your response to my comment asking about your use of ajax, I would suggest you read about the async
option and see how what you're doing might or might not be blocking events.
I would also recommend reading about jQuery ajax callbacks, particularly error
, success
, and complete
(now, with jQuery 1.8+, fail
, done
, and always
).