It's not a scope issue really, it's the fact that $.getJSON() is asynchronous, meaning that this part runs after you return.
Up vote 7 down vote favorite share g+ share fb share tw.
I have the following Javascript function that should return an array of groups that are in database. It uses $.getJSON() method to call get_groups. Php which actually reads from the database.
Function get_groups() { var groups = ; $. GetJSON('get_groups. Php', function(response) { for (var I in response) { groups.
Push(responsei); } } return groups; } Unfortunately, this function does not work as expected because groups. Push(responsei); does not fill the var groups = ; (as I understand it fills some other groups array, probably the global one). Assuming that I don't want to have a global groups variable, how would you solve this problem?
Javascript jquery scope variable-scope getjson link|improve this question asked Aug 17 '10 at 10:43Misha Moroshko5,81823097 99% accept rate.
It's not a scope issue really, it's the fact that $.getJSON() is asynchronous, meaning that this part runs after you return: for (var I in response) { groups. Push(responsei); } You need to call whatever function needs this data in the callback of the asynchronous request, so it runs when the data's available, like this: $. GetJSON('get_groups.
Php', function(response) { var groups = ; for (var I in response) { groups. Push(responsei); } doSomethingThatNeedsGroups(groups); }); Currently you groups array is getting populated, just not when you need it to. If you absolutely have to return this (I strongly recommend using the asynchronous model the way it was intended) you can use the full $.ajax() version and set async:false.
Again...don't go that route if possible, stick to calling whatever function needs the data once it's available, as async: false will lock up the user's browser.
Thanks for the excellent explanation! – Misha Moroshko Aug 17 '10 at 11:13.
Unless you really have a global variable with the name groups (which would be not the best idea actually), you are talking to your "local" groups variable. Since EMCA-/Javascript does have function scope and you are using a closure there, you do have access to that variable. So the problem here, is not the scope.
So even with a global variable with the exact same name, the such called lexical scope will guarantee you the access to your local variable. The actuall problem is, that return groups is executed before $.getJSON() does complete. Since it creates an ajax request, it run asynch.
You should use a callback yourself to process the data: function get_groups(cb) { var groups = ; $. GetJSON('get_groups. Php', function(response) { for (var I in response) { groups.
Push(responsei); } cb. Apply(null, groups); } } get_groups(function(groups){ // do something with groups array }).
Moving the return statement doesn't work, the function will simply return nothing then. – Nick Craver? Aug 17 '10 at 10:51 was fixed before you send that :p – jAndy Aug 17 '10 at 10:53 For your edit, you can do this a bit cleaner with .
Call(null, groups), but the first part of your answer is a bit off...if there's a more local variable with the same name you're always using that variable, a global one will never override a more-locally scoped one. – Nick Craver? Aug 17 '10 at 10:58 @Nick: yay, maybe.
I hoped the second part about function scope makes it clear. – jAndy Aug 17 '10 at 11:03 Thanks a lot! – Misha Moroshko Aug 17 '10 at 11:14.
$. GetJSON('get_groups. Php', function(response) {" is an Callback-Function.
The changes in the groups-Array are affect after the callback is trickerd and also after you return groups. Get_groups enterd request the url and register a callback return groups end of "get_groups" trigger callback anynomous callback-function enterd modifie groups-array end of anynomous callback-function you could not return the modifies of the callback-function directly.
FYI synchronous ajax would solve the problem, but is not recommended – naugtur Aug 17 '10 at 12:55.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.