> for sure the better answer is to do it the proper, async way.... but > you don't really provide any information at all to help others help > you with this
I need to access a JSON array from outside of the scope of the sub- function. I'm not sure how I can explain it more clearly... Basically, I want to do this to save iterating over a specific anchor class more than once. I'll try to provide an example. Here is what I currently have to do: $("a.item").each(function(index){ $(this).doStuff(); }); $.getJSON("example.js", function(json){ for(key in json){ $("a.item:contains(key)").addClass(json[key]); } }); As you can see, that means I am looping over the same set of anchors multiple times, which seems wildly inefficient. I don't want to go synchronously, because this is just a superficial change and shouldn't delay a user's interaction with the page. Maybe the inefficient second loop is the only way to do it. At first glance, it seems moving the doStuff() function inside the getJSON sub-function would be best, but there is no guarantee every anchor will have an associated key, and they all need doStuff(). Any additional thoughts are appreciated.