如何在每个下载时一个接一个地获取JSON个文件
How to get JSON files one after other when each downloaded
我正在尝试先编写一个插件,
- 它加载 2 json 个文件,将每个文件保存在一个变量中,
- 然后再加载 2 个 json 文件并将它们保存在变量中,
- 然后运行另一个函数。
这三个步骤需要在上一个步骤完成后进行。主要原因是从上一步获取的信息将在下一步中使用。
我有这段代码,但不能按我想要的方式工作。
$.when(parseJSON1(), parseJSON2())
.then(
parseJSON3(station_data.dj), parseJSON4(station_data.songurl)
)
.then(
_cacheOptions
)
});
var station_data, history_data, itunes_data, coverList_data;
// Core Functions
function _cacheOptions() {
station_data = stationInfo[0];
history_data = stationHistory[0];
itunes_data = itunesInfo[0];
coverList_data = coverInfo[0];
}
// Functions
function parseJSON1() {
return $.getJSON(settings.JSON1);
}
function parseJSON2() {
return $.getJSON(settings.JSON2);
}
function parseJSON3(searchTerm) {
return $.getJSON(settings.JSON3);
}
function parseJSON4() {
return $.getJSON(settings.JSON4);
}
根据其 documentation,.then() 方法将其参数中的两个回调分别视为在成功或 failure/error 时调用的函数。目前您的代码编写方式,看起来您是在告诉它:
- 获取 JSON1 和 JSON2
- 如果这两个调用成功解析,获取 JSON3
- 如果任一调用失败,获取 JSON4
- 然后调用_cacheOptions
除此之外,似乎还有一些其他问题:
- 在您的 .when() 子句中获取的数据没有传递给需要来自第一步的信息的两个回调。
- _cacheOptions 函数正在引用未定义的变量stationInfo、stationHistory、itunesInfo、coverInfo
假设这四个变量是 parseJSON1 到 4 获取的数据,您可以将代码重写为:
var stationInfo, stationHistory, itunesInfo, coverInfo; // to store the returned JSON data
var station_data, history_data, itunes_data, coverList_data; // set by _cacheOptions
// wait for getJSONs to resolve
$.when(parseJSON1(), parseJSON2())
// use resolved data in next step
.then(function(JSON1, JSON2) {
// set your scoped vars
stationInfo = JSON1;
stationHistory = JSON2;
// Then call the next two async fetches
// Note that, since you already set your local vars with the returned data,
// you don't need to pass anything to these functions, they can just
// reference the info like the _cachOptions function
return $.when(parseJSON3(), parseJSON4());
})
.then(function(JSON3, JSON4){
// set your scoped vars
itunesInfo = JSON3;
coverInfo = JSON4;
return _cacheOptions();
})
// Core Functions
function _cacheOptions() {
station_data = stationInfo[0];
history_data = stationHistory[0];
itunes_data = itunesInfo[0];
coverList_data = coverInfo[0];
}
我正在尝试先编写一个插件,
- 它加载 2 json 个文件,将每个文件保存在一个变量中,
- 然后再加载 2 个 json 文件并将它们保存在变量中,
- 然后运行另一个函数。
这三个步骤需要在上一个步骤完成后进行。主要原因是从上一步获取的信息将在下一步中使用。
我有这段代码,但不能按我想要的方式工作。
$.when(parseJSON1(), parseJSON2())
.then(
parseJSON3(station_data.dj), parseJSON4(station_data.songurl)
)
.then(
_cacheOptions
)
});
var station_data, history_data, itunes_data, coverList_data;
// Core Functions
function _cacheOptions() {
station_data = stationInfo[0];
history_data = stationHistory[0];
itunes_data = itunesInfo[0];
coverList_data = coverInfo[0];
}
// Functions
function parseJSON1() {
return $.getJSON(settings.JSON1);
}
function parseJSON2() {
return $.getJSON(settings.JSON2);
}
function parseJSON3(searchTerm) {
return $.getJSON(settings.JSON3);
}
function parseJSON4() {
return $.getJSON(settings.JSON4);
}
根据其 documentation,.then() 方法将其参数中的两个回调分别视为在成功或 failure/error 时调用的函数。目前您的代码编写方式,看起来您是在告诉它:
- 获取 JSON1 和 JSON2
- 如果这两个调用成功解析,获取 JSON3
- 如果任一调用失败,获取 JSON4
- 然后调用_cacheOptions
除此之外,似乎还有一些其他问题:
- 在您的 .when() 子句中获取的数据没有传递给需要来自第一步的信息的两个回调。
- _cacheOptions 函数正在引用未定义的变量stationInfo、stationHistory、itunesInfo、coverInfo
假设这四个变量是 parseJSON1 到 4 获取的数据,您可以将代码重写为:
var stationInfo, stationHistory, itunesInfo, coverInfo; // to store the returned JSON data
var station_data, history_data, itunes_data, coverList_data; // set by _cacheOptions
// wait for getJSONs to resolve
$.when(parseJSON1(), parseJSON2())
// use resolved data in next step
.then(function(JSON1, JSON2) {
// set your scoped vars
stationInfo = JSON1;
stationHistory = JSON2;
// Then call the next two async fetches
// Note that, since you already set your local vars with the returned data,
// you don't need to pass anything to these functions, they can just
// reference the info like the _cachOptions function
return $.when(parseJSON3(), parseJSON4());
})
.then(function(JSON3, JSON4){
// set your scoped vars
itunesInfo = JSON3;
coverInfo = JSON4;
return _cacheOptions();
})
// Core Functions
function _cacheOptions() {
station_data = stationInfo[0];
history_data = stationHistory[0];
itunes_data = itunesInfo[0];
coverList_data = coverInfo[0];
}