Jquery, ajax() and each(), how to wait untill all info is really loaded?

Posted by Moustard on Stack Overflow See other posts from Stack Overflow or by Moustard
Published on 2010-06-06T10:55:56Z Indexed on 2010/06/06 11:02 UTC
Read the original article Hit count: 189

Filed under:
|
|
|

Hello, I have a function using $.ajax() to get values from an XML file, when the info is loaded and success event is fired, I use $(xml).find('').each(function(){}); to populate some vars...

function getData()
{
    $.ajax({
        type: 'GET',
        url : 'info.xml',
        dataType: 'xml',
        success: function(xml)
        {
            $(xml).find('DATAS').each(function()
            {
                date = new Date($(this).attr('DATE'));
                alert(date);
            })
                    //Here I have a bigger find/each that should take more time 
        },
            error: function()
            {
                    return false;
            }

    }); 
}

In this case, when I trigger the function from the document ready function, the alert shows the right data, but If I remove the alert from the function and try this instead, date wont be defined yet:

$(document).ready(function()
{
if(getData() != false)
{
    alert(date);

    }
});

I guess in this case the data is not ready yet? Is there a way to keep control on when the whole each() traversing is finished and ready?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about Xml