设置和使用变量(JavaScript)
我正在使用FileReader API加载一个文本文件。在此过程中,我将内容放入设置和使用变量(JavaScript),javascript,variables,video,Javascript,Variables,Video,我正在使用FileReader API加载一个文本文件。在此过程中,我将内容放入 // Assign an ontimeupdate event to the video element, and execute a function if the current playback position has changed video.ontimeupdate = function () { duringPlayback() }; function duringPlayback() {
// Assign an ontimeupdate event to the video element, and execute a function if the current playback position has changed
video.ontimeupdate = function () { duringPlayback() };
function duringPlayback() {
document.getElementById("speed").innerHTML = (Math.round(video.playbackRate * 100) / 100).toFixed(2);
// I need to use the var hostFile (reader.result) contents here. ###########
}
// Load Tab Delimted File
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function (e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function (e) {
// Entire file
fileDisplayArea.innerText = reader.result;
var hostFile = reader.result; //###########
}
reader.readAsText(file);
//doIt(); //Normally doIt would be called from another function
} else {
fileDisplayArea.innerText = "File not supported!"
}
});
我想我应该简单地将reader.result(或fileDisplayArea.innerText)内容加载到一个全局变量中
那就这么做吧。在所有内容之外声明变量:
是的,我试过了。(很抱歉,我没有在帖子中捕捉到这一行。)我以前有var hostFile=“”;(我想我必须给它一个初始值。),但已经将它改为var hostFile;现在,控制台返回“undefined”而不是nothing。我还是被难住了。看来这应该很容易!听起来好像是在将主机文件设置为读取器的结果之前调用了回放期间的
。您可能需要设置一个plunker/jsfiddle/codepen来更好地说明您的问题!我只是闻到了自己脑袋里的屁。我在用我的代码一行一行地比较你所拥有的。在reader.onload函数中,我使用了“var hostFile=reader.result”而不是“hostFile=reader.result”。当然,我删除了“var”,现在它可以正常工作了我在这方面还不够好,很容易犯这样愚蠢的错误。非常感谢。如果您没有花时间发布正确的脚本,我不确定捕获错误需要多长时间。我肯定会有一段时间的再次感谢你,安德鲁
var hostFile;
video.ontimeupdate = function () { duringPlayback() };
function duringPlayback() {
document.getElementById("speed").innerHTML = (Math.round(video.playbackRate * 100) / 100).toFixed(2);
// I need to use the var hostFile (reader.result) contents here.
console.log(hostFile);
}
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', function (e) {
var file = fileInput.files[0];
var textType = /text.*/;
if (file.type.match(textType)) {
var reader = new FileReader();
reader.onload = function (e) {
// Entire file
fileDisplayArea.innerText = reader.result;
hostFile = reader.result; //###########
}
reader.readAsText(file);
//doIt(); //Normally doIt would be called from another function
} else {
fileDisplayArea.innerText = "File not supported!"
}
});