javascript - Array.push not updating an array variable -


here code working on:

<!--html code referencing file --> <input type="file" name="file" id="file"> <script> var store = []; document.getelementbyid('file').onchange = function(){     var file = this.files[0];     var reader = new filereader();      // define body of reader function     reader.onload = function(progressevent){      // lines     var lines = this.result.split('\n');     for(var line = 0; line < lines.length; line++){      // store in array     store.push(lines[line]);     //console.log(store.length); // line on being uncommented     // shows store being modified. values getting printed     // 1,2,3, ...... upto 16 (length of input file) } };  // read file , store in var "store" reader.readastext(file); console.log(store.length); // problem appears here!!!!! }; </script> 

the problem is, after choosing file containing 16 sample numbers, console prints store.length value 0. why push command not affecting var "store" ?

filereader asynchronous. either want use filereadersync or this:

var store = [];  document.getelementbyid('file').onchange = function() {    var file = this.files[0];    var reader = new filereader();      // define body of reader function    reader.onload = function(progressevent) {        // lines      var lines = this.result.split('\n');      (var line = 0; line < lines.length; line++) {          // store in array        store.push(lines[line]);      }    };      reader.onloadend = function() {      console.log(store.length);    };      // read file , store in var "store"    reader.readastext(file);  };
<input type="file" name="file" id="file">


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Website Login Issue developed in magento -

Can the constants be defined inside a model file of a framework in PHP? -