[英]Setting and using a variable (JavaScript)
我正在使用FileReader API加载文本文件。 在此过程中,我将内容放置在<pre>
标记文件中: fileDisplayArea.innerText = reader.result;
。 稍后,我想在视频播放期间触发的afterPlayback函数中使用此内容。 video.ontimeupdate = function () { duringPlayback() };
我不想一遍又一遍地将加载文件的内容放入video.ontimeupdate函数中,所以我想我只是将reader.result(或fileDisplayArea.innerText)内容加载到全局变量,以后我可以在video.ontimeupdate函数中的内容上执行所需的操作。 但是我似乎无法设置变量,以便可以在video.ontimeupdate函数中使用它。 我在严格模式下运行,但我对此进行了评论,但仍然无法正常工作。
如何在video.ontimeupdate函数中可以使用的变量中设置reader.result内容? 我很沮丧 (如果可能,我想以严格模式运行。)
预先感谢,安德鲁
// 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;
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!"
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.