问题
I have seen someone has posted a similar question but it doesn't really fit what I'am looking for:
Here is my code:
$(document).ready(function(){
$("#btnTest").bind("click", function(){
a = createFile();
alert(">> "+a); // Displayed returned value;
});
function createFile(){
var type = window.TEMPORARY;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
fs.root.getFile('log.txt', {create: true, exclusive: true}, function(fileEntry) {
alert('File creation successfull!');
return 1; // Return value;
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
return 2; // Return value;
}
}
});
Basically once the createFile() function is called it will return either 1 (success) or 2 (error). Problem is I do not know how I can return the value and call it properly?
Any help would be really good.
回答1:
I would create a global variable in your function called returnValue
and set its value
$(document).ready(function(){
$("#btnTest").bind("click", function(){
a = createFile();
alert(">> "+a); // Displayed returned value;
});
function createFile(){
var returnValue;
var type = window.TEMPORARY;
var size = 5*1024*1024;
window.requestFileSystem(type, size, successCallback, errorCallback)
function successCallback(fs) {
fs.root.getFile('log.txt', {create: true, exclusive: true}, function(fileEntry) {
alert('File creation successfull!');
returnValue = 1; // Return value;
}, errorCallback);
}
function errorCallback(error) {
alert("ERROR: " + error.code)
returnValue = 2; // Return value;
}
return returnValue;
}
});
来源:https://stackoverflow.com/questions/65766037/returning-a-value-from-cordova-file-plugin-when-called-from-a-function