无法访问 GET 函数之外的 var

can't access var outside of get function

本文关键字:var 函数 访问 GET      更新时间:2023-09-26

如何在 javascript 中的数组中保存我从文本文件中检索到的信息以供以后使用?我用它来放置一些 HTML,但也对用户做出反应。到目前为止,我可以使用内联函数调用很好地放置 HTML,但我希望以后使用该数据......

function get_words() {
    var words = new Array();
    var sylls = new Array();
    var csv_file = new Array(); // for word arrays
    $.get('terms.csv', function(data){
        csv_file = data.split(''n');
        // csv file is now in an array, split into seperate word array and syllable array
        for (var i = 0; i < csv_file.length; i++) {
            var both = csv_file[i].split(',');  // split at the comma
            words[i] = both[0]; // populate word array
            sylls[i] = both[1]; // populate syllable array
            put_word(words[i], sylls[i]);
        };
        check_resize();
    });
}
function put_word(word, sylls) {
    console.log(word);
    // place the words into 'words' div
    var divID = document.getElementById("words");   // grab 'words' div
    divID.innerHTML += "<span>" + word + "</span>" + "<sup>" + sylls + "</sup> ";
}

这就是我的代码。如果单词[]和音节[]可以在获取功能之外访问,我希望它。

编辑:让我更清楚(哎呀)。我在哪里声明我的数组并不重要。我知道这一点的原因是因为我可以将它们放在脚本的顶部(函数外部)和 get_words() 的末尾尝试 console.log(words),它将是一个空数组。

var words = new Array();
var sylls = new Array();
var csv_file = new Array(); // for word arrays
$(document).ready(function(){
    get_words();
});

function get_words() {

    $.get('terms.csv', function(data){
            csv_file = data.split(''n');
                // csv file is now in an array, split into seperate word array and syllable array
                for (var i = 0; i < csv_file.length; i++) {
                    var both = csv_file[i].split(',');  // split at the comma
                    words[i] = both[0]; // populate word array
                    sylls[i] = both[1]; // populate syllable array
                    //put_word(words[i], sylls[i]);
                };
                check_resize();
        });
    console.log(words);
}

编辑:有人可以告诉我在哪里回调吗?

function get_words() {

    $.get('terms.csv', function(data){
            csv_file = data.split(''n');
                // csv file is now in an array, split into seperate word array and syllable array
                for (var i = 0; i < csv_file.length; i++) {
                    var both = csv_file[i].split(',');  // split at the comma
                    words[i] = both[0]; // populate word array
                    sylls[i] = both[1]; // populate syllable array
                    //put_word(words[i], sylls[i]);
                };
        });
}

所以......如果我想等到这个文件被放入数组之后,然后调用另一个函数,我该怎么做?

var words = [];
var sylls = [];
function get_words() {
    $.get('terms.csv', function(data){
        // Clear the result arrays
        words = [];
        sylls = [];
        var csv_file = data.split(''n');
        // csv file is now in an array, split into seperate word array and syllable array
        for (var i = 0; i < csv_file.length; i++) {
            var both = csv_file[i].split(',');  // split at the comma
            words[i] = both[0]; // populate word array
            sylls[i] = both[1]; // populate syllable array
            put_word(words[i], sylls[i]);
        };
        check_resize();
    });
}
var words = [], sylls = [], csv_file=[];
function get_words(){ ... }
function put_word(){ ... }
function needs_words_and_sylls(){ .... }

在javascript中,范围被创建并绑定到仅包含它们的函数中。

对于您的情况,您的变量words是在函数get_words中创建的,并且只能在函数内部访问get_words。若要允许两个或多个函数访问同一作用域,必须在同一作用域中定义它们。

同样,对于您的情况,您的两个函数似乎都在全局范围内定义

,因此您希望两个函数都可以访问的变量也需要在全局范围内定义:

var words = new Array();
    var sylls = new Array();
    var csv_file = new Array(); // for word arrays
function get_words() {

    $.get('terms.csv', function(data){
        csv_file = data.split(''n');
        // csv file is now in an array, split into seperate word array and syllable array
        for (var i = 0; i < csv_file.length; i++) {
            var both = csv_file[i].split(',');  // split at the comma
            words[i] = both[0]; // populate word array
            sylls[i] = both[1]; // populate syllable array
            put_word(words[i], sylls[i]);
        };
        check_resize();
    });
}
function put_word(word, sylls) {
    console.log(word);
    // place the words into 'words' div
    var divID = document.getElementById("words");   // grab 'words' div
    divID.innerHTML += "<span>" + word + "</span>" + "<sup>" + sylls + "</sup> ";
}

您可以做一些事情,将变量移出函数,如下所示:

var words = new Array();
var sylls = new Array();
var csv_file = new Array(); // for word arrays
function get_words() {
    //code here
}

或者您可以只将变量添加为窗口对象

function get_words() {
    window.words = new Array();
    window.sylls = new Array();
    window.csv_file = new Array(); // for word arrays
}

我还记得能够只定义没有"var"的变量,它就像窗口一样变得全局。让我知道这是否有效。

function get_words() {
    words = new Array();
    sylls = new Array();
    csv_file = new Array(); // for word arrays
}

由于您正在处理一个异步接口来读取数据,因此您只能在该接口上进一步构建。 因此,基本上您必须将"带出数据"的目标转变为"引入处理"。

您可以将处理函数注入到读取函数中:

function get_words( process ) {
    ...
    $.get(...){ function(data){
       ...
       process(word, syllabs);
    }
}
function put_word(w, s) {
   ...
}
//this is where the action starts:
get_words( put_word );

示例:http://jsfiddle.net/xtofl/Qb867/