【发布时间】:2013-02-12 10:51:59
【问题描述】:
我有以下功能:
downloadProductImage: function(remoteImage, localImageName){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
fileSystem.root.getFile(localImageName, {create: true, exclusive: false}, function(fileEntry) {
var localPath = fileEntry.fullPath;
var ft = new FileTransfer();
ft.download(remoteImage,
localPath, function(entry) {
//RETURN THIS imageURL = entry.fullPath;
}, fail);
}, fail);
}, fail);
}
函数 downloadProductImage() 位于全局 var app = {} 中,因此通过 app.downloadProductImage() 访问。
此函数在循环中运行,我希望返回 imageURL 但似乎无法得到它。我在 var app = {} 之外声明了 global var = imageURL 但是每当我尝试在另一个函数中获取 imageURL 时,第一个循环返回 undefined ,其余的都是正确的。
我不确定为什么第一个循环返回 undefined.. var imageURL 在页面顶部全局声明..
如果我在上面代码中的 //RETURN THIS imageURL = entry.fullPath; 下发出警报(imageURL),它会正确发出警报,只是当我尝试在函数之外访问它时不会发出警报
【问题讨论】:
-
您是否尝试过在对象中将 imageURL 指定为应用程序对象的一部分(即
app.imageURL)并将其设置为self.imageURL,其中var self = this;放置在ft.download行之前。可能有帮助? -
您将无法返回。操作是异步的。
-
在检查顶层的 imageURL 之前是否检查过内部函数已被调用?它可能还没有被调用。几个明智的 console.log() 调用可能会帮助您确保这里没有竞争条件。
标签: javascript function nested