Whenever you want to use the data retrieved from ajax, you must use it in the callback function which is triggered when the ajax call receives the response. This is a common practice.
But if the ajax call to retrieve data resides in a common function, it would be a little more complicated. Below I give an example to explain this.
function DataUtil(callBack) {
var me = this;
$.ajax({
url : "../json/test.json",
success : function(data) {
me.data = $.parseJSON(data);
callBack(me);
}
});
// can only be used in the callBack
me.getName = function() {
return me.data.name;
};
// can only be used in the callBack
me.getDescription = function() {
return me.data.description;
};
}
function displayRetrievedData() {
var dataRetrieved = {};
var callBack = function(dataUtil) {
dataRetrieved.name = dataUtil.getName();
dataRetrieved.description = dataUtil.getDescription();
console.log("in the call back:");
console.log(dataRetrieved);// gets the correct data from the json
};
DataUtil(callBack);
console.log("out the call back:");
console.log(dataRetrieved);// not sure it gets the data from the json. It
// depends the ajax call speed
}
function canNotGetCorrectData() {
var dataRetrieved = {};
var dataUtil = new DataUtil(function() {
});
dataRetrieved.name = dataUtil.getName();// not sure the data is ready
dataRetrieved.description = dataUtil.getDescription();// not sure the data is ready
console.log("Can not get the data:");
console.log(dataRetrieved);
}
displayRetrievedData();
canNotGetCorrectData();
分享到:
相关推荐
(a) filling in skeleton tables of the database with examples of what is to be retrieved (b) placing SQL keywords, such as select, under the column names they want to retrieve (c) typing a ...
When the index is traversed to the leaf level, the data itself has been retrieved, not simply pointed to. Uniqueness Is Maintained In Key Values In SQL Server 2000, all clustered indexes are unique. ...
protocols will help you understand how to use the JavaMail API. While the API is designed to be protocol agnostic, you can't overcome the limitations of the underlying protocols. If a capability isn...
• The user should be able to control how applications use the tablet. The user interface must be ef-ficient, consistent, and customizable. 2 DESIGN GOALS While the tablet interface design must ...
First, the content provider has more control over the user experience, since it has to use few intermediary ISPs. Second, it can save money by sending less traffic into provider networks. Third, if ...
The basic structure of a SELECT statement includes specifying the columns to be retrieved and the table from which to retrieve them. **Selecting Columns in a Table** To select specific columns from ...
time communication, and the local storage API enables data such as high scores or game preferences to be kept on a user’s computer to be retrieved next time they play your game. All of these features...
The structure determines how quickly items can be added, manipulated and retrieved from the container. Using different item handlers, the same structure can provide for quite different containers ...
• How the data is being retrieved, and joined, from the tables defined in your query. • How aggregations in GROUP BY queries are put together. • The anticipated load, and the estimated cost, that ...
The structure determines how quickly items can be added, manipulated and retrieved from the container. Using different item handlers, the same structure can provide for quite different containers ...
In the first column, an uppercase letter indicates how the staging area differs from the last committed state. letter meaning description - no change same in HEAD and index A added not in HEAD, in ...
Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...
用IconMap方法可视化分析从遥感数据提取的蚀变矿物信息,张显峰,麦克派日拉,约束能量最小法(CEM)的亚像元分析算法首先用来从ASTER地面反射率影像数据提取美国加州巧克力山地区的重要蚀变矿物信息。...
time communication, and the local storage API enables data such as high scores or game preferences to be kept on a user's computer to be retrieved next time they play your game. All of these features ...
Minor changes to the scanning code for the filename of attachments.1.8.2- Fixed a bug with the filename of attachments (thanks to Taufer Pavel Ing.).- Added the function IsIPAddress from hou yg (the ...
ment, which allows you to calculate totals on groups of the rows retrieved from the database. This statement looks and feels very much like the straight SQL statements, yet it’s not part of standard ...
And here is the layout template used to build the output from the XML document retrieved from Amazon. It is parsed every time a book record is retrieved, so the output rendering is the fastest ...