$.when().done() not working as expected

I need to load html on to a page via jquery ajax. Then, after the html has loaded, I want to parse it for certain data that will be in the loaded html, and use it to create map markers. So I need the load method to act synchronously, so that the html is definitely loaded before my setMarkers() method tries to parse it.

$(document).ready(function () {
    initializeMap();
    $.when($("#orders").load("datadisp.aspx")).done(function () {
        setMarkers();
    });

Open in new window



I thought the current set up that I have is supposed to do exactly that, but I can tell from the debugger that setMarkers() is still being called before the load has completed, because when I put a breakpoint on setMarkers() and inspect the html, I can tell it has not loaded yet. Can somebody show me the right way to solve this problem? Thanks.
LVL 9
BobHavertyComhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

.load accepts a callback function in the parameters, so you could do the following:

    $("#orders").load("datadisp.aspx", function () {
        setMarkers();
    });

Open in new window


Giannis
hieloCommented:
The reason it does not work is because the .load() method returns a jQuery object.  However, the .when() method expects a Promise or Promise-compatible object:
...the jqXHR object returned by jQuery.ajax() is a Promise-compatible object ...
http://api.jquery.com/jQuery.when/

The post given above should execute setMarkers() after #orders has been populated with whatever content your "datadisp.aspx" file is generating.

Regards,
Hielo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.