Your example is scoped just fine. Your problem is you don't understand the implications of how JavaScript is mainly asynchronous.
Your example is scoped just fine. Your problem is you don't understand the implications of how JavaScript is mainly asynchronous. Let me go through your example to show you.
Your document ready handler runs. It calls riseData. RiseData declares a variable, jsonResult.
RiseData calls $. Ajax, which schedules an AJAX request to be done, but the response will happen later. Since $.
Ajax is usually non-blocking/asynchronous, riseData continues and returns jsonResult. Since jsonResult has not been assigned yet because the AJAX request has not completed, the default value of undefined is returned. In the document ready handler, you end up with alert("Outside: "+undefined);.
An event loop to a few milliseconds later, this happens: The AJAX request is completed. JQuery forwards this on to your callback. JsonResult is set, but riseData has already returned.
The alert inside riseData alerts with the new data, after the document ready handler has already alerted undefined. There are two solutions to this problem. The first solution is simple, but often results in a worse user experience.
This solution is to add the async: false option to $.ajax. This makes $. Ajax block and freeze the browser.
The second solution is to adopt an asynchronous style for almost everything. That means making riseData accept a callback and calling it inside the AJAX response handler. Here's your code transformed with this method: function riseData(callback) { $.
Ajax({ success: function(data, textStatus, jqXHR){ var jsonResult = jqXHR. ResponseText; alert("Inside: " + jsonResult); callback(jsonResult); }, error: function (jqXHR, textStatus, errorThrown) { $('#errLog'). Append('Status: ' + qXHR.
StatusText); } }); } $(document). Ready(function(){ //var intervalID = setInterval('UTCclock()',100); // Unrelated, but it's better to pass a // function into setInterval than a string. Var intervalID = setInterval(UTCclock, 100); riseData(function(jsonResult) { alert("Outside: " + jsonResult); }); }).
AHA! Thanks. So setting... .
Asynch: false,brbr in the $.axaxSetup() fixed it. – RAMilewski May 28 '11 at 7:16 Now all I have to do is master "mini markdown" which seems to change the rules for posting a comment from when you post a post. Inr which adding white space seems impossible....anyway, thanks for the help!
– RAMilewski May 28 '11 at 7:23.
This is only partially a scope issue. Your ajax call is asynchronous, so the riseData() function returns a value before the ajax call is executed. Try setting your variable outside the method.
Var jsonResult; function riseData() { $. Ajax({ ... jsonResult should not only be available in riseData(), but also out. However, you'll still have the issue of exactly when it gets populated.
A link for a little more explanation: JavaScript Variable Scope.
There was no scope issue at all. If $. Ajax was synchronous, it would work just fine.
Thus, no scope issue. – icktoofay May 28 '11 at 6:29 and if . Ajax is not synchronous there is a scope issue if the value is going to be used elsewhere.
Isn't synchronous ajax an oxymoron considering asynchronous is in the ancronym. – user82646 May 28 '11 at 6:32 It's true that if you consider AJAX to strictly be that acronym, then it's an oxymoron, but many people use AJAX for JSON instead of XML, too... Anyway, the OP is returning jsonResult and using the return value of the function. There was no attempted access of jsonResult through the name of jsonResult outside of riseData.
– icktoofay May 28 '11 at 6:34 oh, I thought it was implied by asking about getting jsonResult outside of riseData by this sentence, ..."available to other parts of the script. " Oh, well, I should stay away from these quick answer types. – user82646 May 28 '11 at 6:39.
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.