我使用javascript和skydrive API編寫網站。一個函數在另一個函數完成之前加載
這是我的代碼:
var clientId = '00000000400FFB5A';
var redirectUri = "http://milmantasimmigration.com/skydrive/test/test.html";
WL.init({
client_id: clientId,
redirect_uri: redirectUri
});
WL.Event.subscribe("auth.login", onLogin);
WL.Event.subscribe("auth.sessionChange", onSessionChange);
var session;
var companyFolders;
var reportFolders;
var reports;
session = WL.getSession();
if (session) {
log("You are already signed in!");
getCompanyFolders();
} else {
WL.login({
scope: "wl.signin"
});
}
function onLogin() {
var session = WL.getSession();
if (session) {
log("You are signed in!");
}
}
function onLogout() {
WL.logout();
log("You are logged out!");
}
function onSessionChange() {
var session = WL.getSession();
if (session) {
log("Your session has changed.");
}
}
function getCompanyFolders() {
var getCompanyFoldersDeferred = $.Deferred();
WL.api({
path: "/me/skydrive/shared",
method: "GET"
}).then(
function(response) {
companyFolders = [];
reportFolders = [];
reports = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "folder") {
companyFolders.push(response.data[i]);
getReportFolders(response.data[i].id + "/files/", companyFolders.length - 1);
}
}
$.when.apply($, companyFolders).done(function() {
getCompanyFoldersDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getCompanyFoldersDeferred.promise();
}
function getReportFolders(path, index1) {
var getReportFoldersDeferred = $.Deferred();
//var index2 = 0;
WL.api({
path: path,
method: "GET"
}).then(
function(response) {
reportFolders[index1] = [];
reports[index1] = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "folder") {
reportFolders[index1].push(response.data[i]);
//index2++;
getReports(response.data[i].id + "/files/", index1, reportFolders[index1].length - 1);
}
}
$.when.apply($, reportFolders).done(function() {
getReportFoldersDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getReportFoldersDeferred.promise();
}
function getReports(path, index1, index2) {
var getReportsDeferred = $.Deferred();
//var index3 = 0;
WL.api({
path: path,
method: "GET"
}).then(
function(response) {
reports[index1][index2] = [];
for (var i = 0; i < response.data.length; i++) {
if (response.data[i].type == "file") {
reports[index1][index2].push(response.data[i]);
//index3++;
}
}
$.when.apply($, reports).done(function() {
getReportsDeferred.resolve();
});
}, function(response) {
log("Cannot get files and folders: " + JSON.stringify(response.error).replace(/,/g, ",\n"));
});
return getReportsDeferred.promise();
}
getCompanyFolders().done(function() {
showReportsHTML();
});
function showReportsHTML() {
for (var i = 0; i < companyFolders.length; i++) {
for (var j = 0; j < reportFolders[i].length; j++) {
for (var k = 0; k < reports[i][j].length; k++) {
alert(reports[i][j][k].name);
}
}
}
}
function log(x) {
alert(x);
}
這兩條線
getCompanyFolders();
showReportsHTML();
shoulde執行getCompanyFolders後發揮了一個又一個()函數完成其工作。但是在getCompanyFolders()填充報表3D數組之前執行showReportsHTML()。
如何在getCompanyFolders()完成後啓動showReportsHTML()函數?
異步調用,愛上它們。 – epascarello