Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #32331
| From | Jake Jarvis <pig_in_shoes@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Read in file to array. |
| Date | 2017-01-25 10:29 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <o69r2v$s5e$1@dont-email.me> (permalink) |
| References | (15 earlier) <2f259308-c08a-485f-8007-1cc7d5d64f33@googlegroups.com> <o68hkt$kkh$1@dont-email.me> <c3afa1c1-4382-49ad-a6e5-255ff3dc7fb7@googlegroups.com> <o68t48$prk$1@dont-email.me> <a2e98a1b-a054-4571-88da-5231b3349b5b@googlegroups.com> |
Am 25.01.2017 um 03:51 schrieb jonas.thornvall@gmail.com:
> Den onsdag 25 januari 2017 kl. 01:58:25 UTC+1 skrev Jake Jarvis:
>> Am 25.01.2017 um 00:32 schrieb jonas.thornvall@gmail.com:
>>> Den tisdag 24 januari 2017 kl. 22:42:30 UTC+1 skrev Jake Jarvis:
>>>> Am 24.01.2017 um 22:17 schrieb jonas.thornvall@gmail.com:
>>>>> Den tisdag 24 januari 2017 kl. 20:07:12 UTC+1 skrev jonas.t...@gmail.com:
>>>>>> Den tisdag 24 januari 2017 kl. 18:04:54 UTC+1 skrev jonas.t...@gmail.com:
>>>>>>> Den tisdag 24 januari 2017 kl. 17:54:52 UTC+1 skrev jonas.t...@gmail.com:
>>>>>>>> Den tisdag 24 januari 2017 kl. 17:42:54 UTC+1 skrev Jake Jarvis:
>>>>>>>>> Am 24.01.2017 um 17:24 schrieb jonas.thornvall@gmail.com:
>>>>>>>>>> Den tisdag 24 januari 2017 kl. 15:27:33 UTC+1 skrev Ben Bacarisse:
>>>>>>>>>>> jonas.thornvall@gmail.com writes:
>>>>>>>>>>>
>>>>>>>>>>>> Den måndag 23 januari 2017 kl. 21:36:45 UTC+1 skrev Ben Bacarisse:
>>>>>>>>>>>>> jonas.thornvall@gmail.com writes:
>>>>>>>>>>>>>> Den måndag 23 januari 2017 kl. 20:39:06 UTC+1 skrev jonas.t...@gmail.com:
>>>>>>>>>>>>>>> Den måndag 23 januari 2017 kl. 20:24:35 UTC+1 skrev jonas.t...@gmail.com:
>>>>>>>>>>>>>>>> Den måndag 23 januari 2017 kl. 19:29:54 UTC+1 skrev Michael Haufe (TNO):
>>>>>>>>>>>>>>>>> jonas.t...@gmail.com wrote:
>>>>>>>>>>> <snip>
>>>>>>>>>>>>>> This is my "working with two loads" code but if someone straight away
>>>>>>>>>>>>>> can say the cause why i can't load mytext first time and it say
>>>>>>>>>>>>>> undefined or howto use textFromFileLoade outside the brackets
>>>>>>>>>>>>>> "function?", i am all ears.
>>>>>>>>>>>>>
>>>>>>>>>>>>> Have you posted a link to a site that exhibits the problem? Code
>>>>>>>>>>>>> snippets are unlikely going to get anyone tor reply.
>>>>>>>>>>> <snip>
>>>>>>>>>>>> Well it bascily working,
>>>>>>>>>>>
>>>>>>>>>>> Ah, OK. That's another reason you may not get many responses. You post
>>>>>>>>>>> so many followups to yourself that people might get the idea you post
>>>>>>>>>>> first and investigate later. Anyone looking at a problem might feel they
>>>>>>>>>>> are wasting their time since you'll fix it yourself in the man time.
>>>>>>>>>>>
>>>>>>>>>>> I see you posted more code with no URL.
>>>>>>>>>>>
>>>>>>>>>>> <snip>
>>>>>>>>>>> --
>>>>>>>>>>> Ben.
>>>>>>>>>>
>>>>>>>>>> Well i don't think it is a bug problem the problem is that i do not understand how the invoked function do work. I simply do not get how a variable within the invoked function "textFromFileLoaded" manage to write the data to a textarea. But when i try to use it after the invoked? function code it is undefined. So as you can see i tried to copy it to a global variable mytext. Problem is when mytext is called after the function it is undefined.
>>>>>>>>>>
>>>>>>>>>> So i understand, that i just do not understand how this type of invoked functions work, in any normal case the global variable mytext could not be undefined. I have not the faintest idea what is going on, so there is nothing to look for.
>>>>>>>>>>
>>>>>>>>>> function o_gush(){
>>>>>>>>>> etc
>>>>>>>>>> fileReader.onload = function(fileLoadedEvent)
>>>>>>>>>> {
>>>>>>>>>> var textFromFileLoaded = fileLoadedEvent.target.result;
>>>>>>>>>> document.debug.data.value = textFromFileLoaded;
>>>>>>>>>> mytext=textFromFileLoaded;
>>>>>>>>>> };
>>>>>>>>>> fileReader.readAsText(fileToLoad, "UTF-8");
>>>>>>>>>> **do** something withh mytext----->undefined WHY????????????????? It wrote to textarea usin textFromFileLoeded yet neither textFromFileLoaded nor mytext exist outside paragraph?????? Well unless you load again..........
>>>>>>>>>> etc}
>>>>>>>>>>
>>>>>>>>>
>>>>>>>>> Have you considered sprinkling your code with some debug messages, so as
>>>>>>>>> to make sure the flow of the program is what you think it ought to be?
>>>>>>>>>
>>>>>>>>> When you write "try to use it after the", what do you mean exactly?
>>>>>>>>> Time or location?
>>>>>>>>
>>>>>>>> Well i always imagined that code is executed linear line after line and if there is a paragraph with loops or function calls, after the code inside the paragraph executed the code below it is executed. That is my general understanding of programming languages that things return after the paragraph code executed. If this is not the case with javascript, i do wonder what this new paradigm called and who come up with it, because it really isn't obvious.
>>>>>>>>
>>>>>>>> So where do you suggest the program go after the fileloading paragraph executed, because i am really out of knowledge on that part?
>>>>>>>>
>>>>>>>> And if it is not to 5he line of code below it after paragraph i seriously doubt the sanity of the one who implemented this invoked function call feature. Things should be transparent not upto guess work or bible studies.
>>>>>>>
>>>>>>> Just to be clear about it if i call the function a second time, it will execute the code "below" brackets/paragraph of fileloading. So for me it is a mystery, i suggested it may be something with IRQ and timing of file loading? Because it is and outside javascript call?
>>>>>>
>>>>>> I guess file read and write is system calls and the code below is for some reason executed before the system actually fetched and transfered the data into the variable. ***I think that is what happens***. And at second call there is no problem because the data is already there. I even tried a dummy loop doing something before accessing mytext but it did not help it was basically a counter up to a million adding the counter values, maybe it simply was to fast executed.
>>>>>>
>>>>>> Yeah i know that it indeed do sound weird, but if that is the case can you do anything about it?
>>>>>
>>>>> Well i could probably change function to call and let that function call loadGraph two times, but as i say really do not understand what is happening, as soon any of the skilled programmers tell me that it is a timing issue with system call i will think up something.
>>>>>
>>>>> For me it seem hard to beleive it is some bug in code that dissapear the second time you load the function, it seem the part that load the I/O data is not in phase with the runtime execution of the script.
>>>>>
>>>>> One would thought it should had waited and data acknowledged loaded, but maybe the processes somehow run in different threads with priorities.
>>>>>
>>>>>
>>>>
>>>> The bug is in your thinking ...
>>>>
>>>>
>>>> I'll quote from the "File API" document linked from
>>>> developer.mozilla.org (= MDN = Mozilla Developer Network):
>>>>
>>>> <quote>
>>>> File or Blob reads should happen asynchronously on the main thread, with
>>>> an optional synchronous API used within threaded web applications. An
>>>> asynchronous API for reading files prevents blocking and UI "freezing"
>>>> on a user agent’s main thread. This specification defines an
>>>> asynchronous API based on an event model to read and access a File or
>>>> Blob’s data. A FileReader object provides asynchronous read methods to
>>>> access that file’s data through event handler content attributes and the
>>>> firing of events. The use of events and event handlers allows separate
>>>> code blocks the ability to monitor the progress of the read (which is
>>>> particularly useful for remote drives or mounted drives, where file
>>>> access performance may vary from local drives) and error conditions that
>>>> may arise during reading of a file.
>>>> </quote>
>>>>
>>>>
>>>>
>>>>
>>>> https://developer.mozilla.org/en-US/docs/Web/API/FileReader#Specifications
>>>>
>>>> https://w3c.github.io/FileAPI/#intro
>>>
>>> First of all that is pretty gritty to read and understand even if i was an englishman and really good js programmer, but i think i get the gist of it. But feel free to explain it in layman terms.
>>>
>>> It is about multitasking, a way of script to execute for example calculate calculate and draw on screen while you are loading the file. I am fine with that, but it is not that cute that it start to execute a new codeblock in the same function before the value read in i mean should one really expect things to be done with the variable before the data read into it. Must there not be like an acknowledge after data read in?
>>>
>>> I mean if you have a variable that is meant to receive data during a file load shouldn't it not wait with operations upon that data until the data is read into it, it is filedata it only have use when it is fully read into the variable?
>>>
>>> It really does not seem like something the user should be expected to handle. Any copy or mannipulation of that data variable should have to wait for an acknowledge, that it is all peachy to do operations upon it?
>>>
>>> I do not see the logic, and i do not see a solution, an eventhandler upon the variable? What should that listen for?
>>>
>>>
>>>
>>
>> You have to accept that the interface of FileReader is what it is, it
>> loads files asynchronously (one reason given is freezing of browser).
>>
>> A solution is to start your computations from within the load callback
>>
>> // load event handler
>> fileReader.onload = function (event) {
>> // file has finally loaded
>> // we know because this function got called
>> // now you can access the result
>> doStuff(event.target.result);
>> };
>>
>>
>> JFTR, if you look through the File API, you'll find there's another
>> interface FileReaderSync for use in Web Workers, which loads files
>> synchronously.
>
> Well copy the code inside works marvelous so a big thank you, but can you explain why? Is the code within the brackets/clause somehow run independently?
> "Well i get that the I/O run asynchronous as you say, but evidently it is aware about the code within brackets?
>
> My writes to file "txt and jpg" cause a reload of page does that mean that my filewrites does not run in asynchronous mode?
>
> I must say i have hard to wrap my head around it, is all the code within brackets run in a concurrent thread to the script?
>
Given
fileReader.onload = function () { /*code*/ };
the "code between brackets" is actually in the body of a function,
none of that code gets run unless that function is actually called.
So you hand the FileReader a function, which it will sometime later
call.
Maybe fiddle some with the progress event and appropriate files.
...
fileReader.onprogress = function (event) {
console.log("Loaded " + event.loaded + " of " + event.total);
};
fileReader.readAsText(file, "UTF-8");
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:29 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 03:59 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 06:26 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 07:41 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:08 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 08:43 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 09:41 -0800
Re: Read in file to array. "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2017-01-23 10:29 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:24 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:38 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 11:56 -0800
Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-23 20:36 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 19:54 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 20:10 -0800
Re: Read in file to array. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-24 14:27 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:24 -0800
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 17:42 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 08:54 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 09:04 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 11:07 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 13:17 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 21:35 +0000
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-24 22:42 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:32 -0800
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 15:55 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:59 +0000
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 01:58 +0100
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-24 18:51 -0800
Re: Read in file to array. Jake Jarvis <pig_in_shoes@yahoo.com> - 2017-01-25 10:29 +0100
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-25 08:58 +0000
Re: Read in file to array. jonas.thornvall@gmail.com - 2017-01-23 12:28 -0800
Re: Read in file to array. Tim Streater <timstreater@greenbee.net> - 2017-01-24 17:21 +0000
csiph-web