Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以在此代码上创建多个函数?_Javascript_Html - Fatal编程技术网

Javascript 是否可以在此代码上创建多个函数?

Javascript 是否可以在此代码上创建多个函数?,javascript,html,Javascript,Html,在这里,我想尝试为代码的某些部分创建多个函数。但我不知道怎么做。当我创建多个函数时,输出不起作用 因为这是关于load file.txt的,所以您可以加载一个随机的txt文件,或者在txt中填入一些数字 代码如下: 我能把它改成这个代码吗 function file() { //change it like this? var loadFile = document.getElementById('loadFile'); loadFile.addEventListener('c

在这里,我想尝试为代码的某些部分创建多个函数。但我不知道怎么做。当我创建多个函数时,输出不起作用

因为这是关于load file.txt的,所以您可以加载一个随机的txt文件,或者在txt中填入一些数字

代码如下:

我能把它改成这个代码吗

function file() { //change it like this?
    var loadFile = document.getElementById('loadFile'); 
    loadFile.addEventListener('change', function(e) { 
        var file = loadFile.files[0]; 
        var textType = /text.*/; 

        if (file.type.match(textType)) { 
            var read = new FileReader();
            read.readAsText(file); 
        }
    }
}

function result(e) {
    var result = read.result;
    circle(350, 100, 30, "yellow", "blue", result);
    file();
}

result();

我想你对函数的顺序有些混乱

// this guy should in "result" scope as well
var read;

function file() { //change it like this?
    var loadFile = document.getElementById('loadFile'); 
    loadFile.addEventListener('change', function(e) { 
      var file = loadFile.files[0]; 
      var textType = /text.*/; 

      if (file.type.match(textType)) {
          read = new FileReader();
          read.readAsText(file);

          read.onload = result;
      }
    });
}

function result(e) {
  var result = read.result;
  circle(350, 100, 30, "yellow", "blue", result);
  ... more stuff here
}

// on window load read the file
window.onload = file;
// this guy should in "result" scope as well
var read;

function file() { //change it like this?
    var loadFile = document.getElementById('loadFile'); 
    loadFile.addEventListener('change', function(e) { 
      var file = loadFile.files[0]; 
      var textType = /text.*/; 

      if (file.type.match(textType)) {
          read = new FileReader();
          read.readAsText(file);

          read.onload = result;
      }
    });
}

function result(e) {
  var result = read.result;
  circle(350, 100, 30, "yellow", "blue", result);
  ... more stuff here
}

// on window load read the file
window.onload = file;