从函数调用时从Cordova文件插件返回值 [英] Returning a value from Cordova File Plugin when called from a function

查看:69
本文介绍了从函数调用时从Cordova文件插件返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到有人发布了类似的问题,但它确实不符合我的需求:

I have seen someone has posted a similar question but it doesn't really fit what I'am looking for:

这是我的代码:

$(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;
    }
    }   
});

基本上,一旦调用createFile()函数,它将返回1(成功)或2(错误).问题是我不知道如何返回该值并正确调用它?

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.

推荐答案

我会在您的函数中创建一个名为 returnValue 的全局变量并设置其值

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;
  }
    
});

这篇关于从函数调用时从Cordova文件插件返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆