Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  44] [ 4]  / answers: 1 / hits: 27915  / 12 Years ago, sun, june 10, 2012, 12:00:00

I'm experiencing a problem of $.get function.
The url contains JSON



my code:



 xyz = null

$.get('http://www.someurl.com/123=json', function(data) {
var xyz = data.positions[0].latitude;
});

alert(xyz);
//some more code using xyz variable


I know that xyz will alert a null result because the $.get is asynchronous.



So is there any way I can use the xyz outside this get function?


More From » jquery

 Answers
29

The real answer is NO, but you can use this:



function useXYZ(){
alert(xyz);
}

xyz = null

$.get('http://www.someurl.com/123=json', function(data) {
xyz = data.positions[0].latitude;
useXYZ();
});

[#85004] Saturday, June 9, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
willieelisham

Total Points: 201
Total Questions: 108
Total Answers: 106

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
willieelisham questions
Wed, Apr 14, 21, 00:00, 3 Years ago
Wed, Mar 31, 21, 00:00, 3 Years ago
Sun, Oct 11, 20, 00:00, 4 Years ago
Sat, May 9, 20, 00:00, 4 Years ago
;