无法访问 GET 函数之外的 var
can't access var outside of get function
如何在 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/
相关文章:
- 从var向代码隐藏函数传递值
- 如何将具有文本类型值的var放入jQuery函数中
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- javascript中对象构造函数中的var属性与this.properties
- 函数中this和var之间的区别
- JavaScript-的含义:var DndUpload=函数(inputElem){};
- 在jquery函数内部设置来自jquery函数的var;t运行
- 一个'var'在函数内部声明
- 根据函数参数将CSS值存储在var中,然后对其进行操作
- 如何知道函数的参数中传递的var名称
- 如何在函数内部为jquery工具提示调用不同的var字符串
- JS不工作:Sublime 3编辑器-Var main=函数()-=不是“”;激活“;(功能新手)
- var 函数名称与函数闭包中的名称分配
- 下划线 -> var _ 函数
- 什么's var函数和javascript中的函数之间的区别
- 在另一个文件中的函数中访问var函数
- 如何在javascript中测试var函数
- Javascript中var/函数定义的最大数量
- 如果一个变量没有"var"函数内的关键字
- 将$scope函数转换为常规var函数.对性能有任何影响吗