簡體   English   中英

如何從javascript中的回調函數返回變量?

[英]How do I return a variable from a callback function in javascript?

我需要返回tempVar的值,但我無法弄清楚如何執行此操作,因為它是回調的結果。 處理這樣的事情的正確方法是什么? 我不確定如何說出這個問題。 我希望通過做一些像var tempReturned = readPWFile('filename.txt'); 但即使我在回調的某個地方有一個'返回',這也不是出於顯而易見的原因。 我的主要目標是將txt文件的結果返回給變量。 有人能指出我正確的方向嗎?

function readPWFile(fileName) {
    var tempVar;
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
        fileSystem.root.getFile(fileName, null, gotReadFileEntry, fail);
    });

    function gotReadFileEntry(fileEntry) {
        fileEntry.file(gotFile, fail);
    }

    function gotFile(file) {
        readDataUrl(file);
    }

    function readAsText(file) {
        var reader = new FileReader();
        reader.onloadend = function (evt) {
            tempVar = evt.target.result;
        };
        reader.readAsText(file);
    }
}

您應該通過API提供回調,它將通過將其作為回調參數傳遞來授予對變量的訪問權限。

另一種使用方法是使用promises,它允許你處理一個可能還沒有結果的對象。

例如,你的函數聲明應該是

function readPWFile(fileName, callback) {

並將被調用

readPWFile(filename, function(tempVar) {
     alert("Successfully received tempVar");
});

實質上,您的代碼只是將此回調函數綁定到閱讀器,而不是使用您的代碼。 除非你想在某種程度上改變結果:

function readPWFile(fileName, callback) {
    ...

    function readAsText(file) {
        var reader = new FileReader();
        // Preference the user's passed in callback function over the previous implementation
        reader.onloadend = callback;
        reader.readAsText(file);
    }
}

一個好的開始是閱讀Node.js. Node的目標之一是通過使用回調模式提供非阻塞I / O解決方案。 因此,Node.js社區中的回調模式有很多材料。

您可以嘗試其他解決方案

function getContentFile(filePath){

    var FileToOpen = function FileToOpen(filePath)
    {
        if(window.XMLHttpRequest) var obj = new XMLHttpRequest(); //Firefox, Opera,...

        else if(window.ActiveXObject) var obj = new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer 

        else return(false);


        if (obj.overrideMimeType) obj.overrideMimeType("text/html"); //Avoids bug with Safari


        obj.open("GET", filePath, false);
        obj.send(null);

        if(obj.readyState == 4) return(obj.responseText);
        else return(false);
    }

    var content = FileToOpen(filePath); 
    return content;
}

我使用這個功能,我讀了一個HTML文件。

並像這樣調用這個函數

var contentFile = getContentFile(yourFilePath/file.html);

傳遞一個回調,以便在值更新時調用:

function readPWFile(fileName, cb) {
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
    fileSystem.root.getFile(fileName, null, gotReadFileEntry, fail);
  });
  function gotReadFileEntry(fileEntry) {
    fileEntry.file(gotFile, fail);
  }
  function gotFile(file) {
    readDataUrl(file);
  }
  function readAsText(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
      cb(evt.target.result);
    };
    reader.readAsText(file);
  }
}

用法:

readPWFile("test", function(val) {
  console.debug("Value: " + val);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM