Callback and scope

I ran into some problem to fully understand the scope of callbacks when trying to learn JQuery. I would add that I have little experience with the Javascript language. Code:

var globDom;    // placeholder for DOM fragment

// Getting xml file; jquery parses the file and give me back a DOM fragment
// saveXML is the callback

$.get(requestTarget, {}, saveXML);

// the globDom here is UNDEFINED !

alert(globDom);

// the callback

function saveXML(xmlDom)
{
    globDom = xmlDom;
}

      

I'm sure there are BIG misunderstandings here ...

Are there any suggestions?

thank

Daniel


A-A. Very clearly, I perfectly understand the error in my assumption: I forgot to take into account the time delay between the time the data was accessed (the previous one!) And the time when the content becomes available. Very serious bug - batch programming is biased :-( The problem is clear and now I can correct it correctly. Thanks a lot for the answer!

0


source to share


4 answers


What Herms said. your (simplified) timeline looks like



0.001 globdom = undefined;
0.002 $.get(somerequest, callback)
0.003 alert(globdom)

.. 50 milliseconds later ..

0.053 saveXml(xmlDom)

      

0


source


I haven't used jquery myself, but I understand that a lot of the things it does are asynchronous. The get method is probably not calling your callback immediately. You will need to wait until the callback is triggered before trying to access the globDom.



Try putting a warning in your callback method. Does this happen before or after the warning you put after get ()?

+6


source


Basically, you need to put any code that works with the returned data, either in the callback itself or in functions called from the callback, because the data doesn't exist before.

For example:

// Getting xml file; jquery parses the file and give me back a DOM fragment
// saveXML is the callback

$.get(requestTarget, {}, saveXML);

// the callback

function saveXML(xmlDom)
{
    // the xmlDom here is DEFINED !

    alert(xmlDom);
}

      

+4


source


I think your rating is ok. saveXML creates a closurewhich includes globDom in scope. Closure, if you are not familiar with the term, means that the scope of a function is defined within the scope of that function body. This means that since globDom is in the scope where saveXML was specified, the body of saveXML can access the same globDom instance as the warning defined above.

I think your actual problem is that $ .get is not synchronous. $ .get returns immediately before the fetch completes, which means the call is called before the callback is executed. Instead, you can use $ .ajax and set the "async" parameter to false, which will cause the $ .ajax call to not return until the server request succeeds.

However, the best solution is to make your code asynchronous so that you give up control back to the browser, so the user can still interact with the page while they wait for the server request to complete.

+1


source







All Articles