简体   繁体   中英

This javascript code is running properly without the for loop but not with it.Any help, I have tried everything

Firstly, I have inputted this file and separate all the lines be split() function.Then, I have saved the location( var lat and lng ) and soil type ,but the problem is if I do not use 'for' loop and check whether my code without this loop is working or not , then it is working. This is the link to the text file which i want to read :

https://drive.google.com/file/d/0B_NBRSiUy-R1ZmhwOVhCZFQ4cWc/view?usp=sharing

Please Help.

<html>
<head>
</head>

<body>
<input type="file" id="fileinput" />
<script type="text/javascript">
  function readSingleFile(evt) {
    var f = evt.target.files[0]; 

    if (f) {
      var r = new FileReader();
      r.onload = function(e) { 
          var contents = e.target.result;


        var lines = contents.split('\n');
        var lng = new Array();
        var lat = new Array();
        var soil = new Array();
        var splitted = new Array();
        var k;

        for(var i=0;i<lines.length;i++){
        splitted = lines[i].split(" ");
        lng[i] = splitted[0];
        lat[i] = splitted[1];
        k = splitted.length - 5;
        document.write("First Step Done");
        switch(k){
        case 1: 
                soil[i]= splitted[5]; 
                break;
        case 2: 
                soil[i]= splitted[5] + " " + splitted[6]; 
                break;
        case 3: 
                soil[i]= splitted[5] + " " + splitted[6] + " " + splitted[7]; 
                break;
        case 4: 
                soil[i]= splitted[5] + " " + splitted[6] + " " + splitted[7] + " " + splitted[8]; 
                break;
        }

      }
      document.write("done");

      r.readAsText(f);
    } else { 
      alert("Failed to load file");
    }
  }

          document.getElementById('fileinput').addEventListener('change',readSingleFile, false);
</script>
</body>
</html>

you are missing a curly bracket:

    document.write("done");
};                              // <-- this is missing
r.readAsText(f);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM