Reputation: 515
Declare global variables(jquery):
$.mynamespace = { myVar : "something" };
I will do some operations with xml, after that will assign some other value to myVar and call to function test() will again change the value of myVar. Then I want the value of var must be same as I changed in test() function.
$(document).ready(function(){
//Some XML oprations
$.get("students.xml",{},function(xml){
$.mynamespace.myVar="ewewewewewew";
test();
});
//value of $.mynamespace.myVar must be test
alert($.mynamespace.myVar);
});
function test(){
alert($.mynamespace.myVar );
$.mynamespace.myVar="test";
}
Upvotes: 1
Views: 1642
Reputation: 20286
Ajax stands as Asynchronous JavaScript and XML which means that the calls are Asynchronous. When AJAX is done the sucessful function is called. It can be called anytime. So when javascript reaches the code it just goes through and when ajax is ready success function is called.
There are 2 solutions.
async: false
which makes Ajax NO ajax :) but some kind of SJAX.Upvotes: 1
Reputation: 3621
You can use closures instead of global vars to do the same thing
(function($){
var myNs = {};
$(document).ready(function(){
//Some XML oprations
$.get("students.xml",{async: false},function(xml){
myNs.myVar="ewewewewewew";
test();
});
//value of $.mynamespace.myVar must be test
alert(myNs.myVar);
});
function test(){
alert(myNs.myVar );
myNs.myVar="test";
}
})(jQuery);
Upvotes: 0