Monday, August 31, 2015

Javascript function executing before jQuery finished loading data

It's been a while. Mostly because I haven't done anything serious that has taken me too much work to figure out. So here is the situation:

I'm bringing data to my web page from a Web Service and I store it in local storage, so I can use it offline. The problem? Some of that data is used to load dropdowns. But when I execute the dropdown loading function some of the data is not yet in the local storage. I assume the jQuery is executing asynchronous to the dropdown loading function, or any other Javascript function for that matter. I will have to find some way to confirm all the data is in the local storage, then execute the load dropdown.

EDIT 1: Well I solved THAT quickly! I had a function which parsed all the data then that function called three more functions which loaded the respective dropdown with their data as parameters. What I did was eliminate that function and only kept the three which were being called. The data I was loading there I loaded directly in the respective function. And, the most important, used a function in the .ajax jQuery called "complete", which executes after the success and failure. In each complete, I executed the dropdown loading function respective to each one.