Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #32263 > unrolled thread

Read in file to array.

Started byjonas.thornvall@gmail.com
First post2017-01-23 03:29 -0800
Last post2017-01-24 17:21 +0000
Articles 12 on this page of 32 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#32313

Fromjonas.thornvall@gmail.com
Date2017-01-24 13:17 -0800
Message-ID<2f259308-c08a-485f-8007-1cc7d5d64f33@googlegroups.com>
In reply to#32307
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. 

[toc] | [prev] | [next] | [standalone]


#32314

FromTim Streater <timstreater@greenbee.net>
Date2017-01-24 21:35 +0000
Message-ID<240120172135245529%timstreater@greenbee.net>
In reply to#32313
In article <2f259308-c08a-485f-8007-1cc7d5d64f33@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

>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. 

I already explained what is likely going on and what you should do
about it. Can't you read?

-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens

[toc] | [prev] | [next] | [standalone]


#32315

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2017-01-24 22:42 +0100
Message-ID<o68hkt$kkh$1@dont-email.me>
In reply to#32313
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

[toc] | [prev] | [next] | [standalone]


#32318

Fromjonas.thornvall@gmail.com
Date2017-01-24 15:32 -0800
Message-ID<c3afa1c1-4382-49ad-a6e5-255ff3dc7fb7@googlegroups.com>
In reply to#32315
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?


[toc] | [prev] | [next] | [standalone]


#32319

Fromjonas.thornvall@gmail.com
Date2017-01-24 15:55 -0800
Message-ID<bd3114ef-791a-4d43-bb2d-09d9a473beb5@googlegroups.com>
In reply to#32318
Den onsdag 25 januari 2017 kl. 00:32:47 UTC+1 skrev jonas.t...@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?

So back to the question, how can i assure that the file has loaded before any further code try to copy or manipulate textFromFileLoaded?

Because the normal thing is that you want to manipulate it, and how it manage to copy the content to the textarea is beyond beleif. And even more beyond beleif is that when you nextline try to see the content it do copy to the textarea the variable is still undefined..........

Yeah how the fuck did that copy happen to the textarea of textFromFileLoaded if the variable textFromFileLoaded the next row is still undefined, could someone please explain that?

[toc] | [prev] | [next] | [standalone]


#32330

FromTim Streater <timstreater@greenbee.net>
Date2017-01-25 08:59 +0000
Message-ID<250120170859252590%timstreater@greenbee.net>
In reply to#32319
In article <bd3114ef-791a-4d43-bb2d-09d9a473beb5@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

>So back to the question, how can i assure that the file has loaded before any
>further code try to copy or manipulate textFromFileLoaded?

Use an onload function as you have done but you need to use it properly
as I and others have explained.

-- 
"A committee is a cul-de-sac down which ideas are lured and then 
 quietly strangled." - Sir Barnett Cocks (1907-1989)

[toc] | [prev] | [next] | [standalone]


#32322

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2017-01-25 01:58 +0100
Message-ID<o68t48$prk$1@dont-email.me>
In reply to#32318
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.

[toc] | [prev] | [next] | [standalone]


#32323

Fromjonas.thornvall@gmail.com
Date2017-01-24 18:51 -0800
Message-ID<a2e98a1b-a054-4571-88da-5231b3349b5b@googlegroups.com>
In reply to#32322
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?

[toc] | [prev] | [next] | [standalone]


#32331

FromJake Jarvis <pig_in_shoes@yahoo.com>
Date2017-01-25 10:29 +0100
Message-ID<o69r2v$s5e$1@dont-email.me>
In reply to#32323
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");

[toc] | [prev] | [next] | [standalone]


#32329

FromTim Streater <timstreater@greenbee.net>
Date2017-01-25 08:58 +0000
Message-ID<250120170858097987%timstreater@greenbee.net>
In reply to#32318
In article <c3afa1c1-4382-49ad-a6e5-255ff3dc7fb7@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

>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.

Yes it is, it's very "cute" and it has been explained to you why that
is.

>Must there not be like an acknowledge after data read in?

That is what the onload function is.

-- 
"Once you adopt the unix paradigm, the variants cease to be a problem - you
bitch, of course, but that's because bitching is fun, unlike M$ OS's, where 
bitching is required to keep your head from exploding." - S Stremler in afc

[toc] | [prev] | [next] | [standalone]


#32279

Fromjonas.thornvall@gmail.com
Date2017-01-23 12:28 -0800
Message-ID<6ca890bf-4f64-41f7-9ec6-2f69b6db7e91@googlegroups.com>
In reply to#32276
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:
> > > 
> > > > [... code ...]
> > > 
> > > copy and paste your JavaScript into here and fix the problems you see:
> > > 
> > > <http://jshint.com/>
> > 
> > Well i think i can see what is going on but i just don't understand the inline functions within functions.
> > What i probably wan't is to make textFromFileLoaded visible outside the {} paragraphs, but i am a bit lost i really do not understand this code.
> > 
> > I find it strange though that i can not define a global variable mytext and copy textFromFileLoaded to it? And why does it work the second time.
> > 
> >   fileReader.onload = function(fileLoadedEvent) 
> >     { 
> >         textFromFileLoaded = fileLoadedEvent.target.result; 
> >         document.debug.data.value = textFromFileLoaded;
> >         mytext=textFromFileLoaded; 
> >         
> >     };
> 
> But the good part load graph works i just have to push load button two times ;)

function loadGraph()
{   
    
    var fileToLoad = document.getElementById("fileToLoad").files[0];
    var fileReader = new FileReader();
    fileReader.onload = function(fileLoadedEvent) 
    {
        var textFromFileLoaded = fileLoadedEvent.target.result;
        document.debug.data.value = textFromFileLoaded;
        mytext=textFromFileLoaded;
        
    };
    fileReader.readAsText(fileToLoad, "UTF-8");
    //alert(mytext);
    var sub=mytext.split("!");
    for (var m = 0; m <sub.length ; m ++ )
    {
      arr[m] ={nodelinks : [], hold : false, rposX : 0, rposY : 0, rwidth : 10, rheigth : 10, rcolor : "red" , bcolor : "green"};
    }
    var x=0;
    while (x<sub.length){
       var dataitem=[];
       dataitem=sub[x].split("*");
       //alert(sub[x]);
          var a=0;
        while(a<dataitem.length){
          var links=dataitem[1].split(",");
          var b=0;
          while (b<links.length) {
             arr[x].nodelinks[b]=parseInt(links[b]);
             b++;
          }
          arr[x].hold=false;
          arr[x].rposX=parseInt(dataitem[3]);
          arr[x].rposY=parseInt(dataitem[4]);
          arr[x].rwidth=parseInt(dataitem[5]);
          arr[x].rheigth=parseInt(dataitem[6]);
          arr[x].rcolor=dataitem[7];
          arr[x].bcolor=dataitem[8];
          a++; 
        } 
    x++;
    }
   texten="";
 for (var m = 0; m < sub.length; m ++ ) {
       texten+=m+"*"+arr[m].nodelinks+"*"+arr[m].hold+"*"+arr[m].rposX+"*"+arr[m].rposY+"*"+arr[m].rwidth+"*"+arr[m].rheigth+"*"+arr[m].rcolor+"*"+arr[m].bcolor+"!";
 }
 document.debugtwo.datatwo.value = texten;
}

[toc] | [prev] | [next] | [standalone]


#32303

FromTim Streater <timstreater@greenbee.net>
Date2017-01-24 17:21 +0000
Message-ID<240120171721187975%timstreater@greenbee.net>
In reply to#32266
In article <30277451-ca92-4bc3-b070-3ed4fd8d2dfc@googlegroups.com>,
<jonas.thornvall@gmail.com> wrote:

[snip]

>Well now the gist seem to work but there must be some problem with javascript
>I manage to get an alert on the data if i "load twice", isn't that strange.
>  
>alert(textFromFileLoaded);
>sub=textFromFileLoaded.split("!");
>
>"X3LOADSAVE.js:46 Uncaught ReferenceError: textFromFileLoaded is not defined
>    at loadGraph (X3LOADSAVE.js:46)
>    at HTMLButtonElement.onclick (0GRAPH.HTML?filename=N8L3circle.txt:31)"
>
>But second time file loaded it is read into array without errors reported, it
>seem like a timing issue?
>
>There is no problem to just write to the textarea first time but for some
>reason i can not use textFromFileload at all i must copy the value to my own
>variable and even then it will not be read in the first time i load, but the
>second time i press button to load it work.
>
>function loadGraph()
>{   
>    textFromFileLoaded="";
>    var fileToLoad = document.getElementById("fileToLoad").files[0];
>    var fileReader = new FileReader();
>    fileReader.onload = function(fileLoadedEvent) 
>    {
>        textFromFileLoaded = fileLoadedEvent.target.result;
>        document.debug.data.value = textFromFileLoaded;
>        mytext=textFromFileLoaded;
>    };
>    fileReader.readAsText(fileToLoad, "UTF-8");
>    //arr=[];  
>    
>    alert(mytext);
>    sub=mytext.split("!");
>    for (var x=0;x<sub.length;sub++){
>       dataitem=sub[x].split("*");
>       alert(dataitem[1]);
>       for(var a=0;a<dataitem.length;a++){
>          arr[a].hold=false;
>          arr[a].rposX=parseInt(dataitem[4]);
>          arr[a].rposY=parseInt(dataitem[5]);
>          arr[a].rwidth=parseInt(dataitem[6]);
>          arr[a].rheigth=parseInt(dataitem[7]);
>          arr[a].rcolor=dataitem[8];
>          arr[a].bcolor=dataitem[9];
>          links=dataitem[1].split(",");
>          for (var b=0;b<dataitem;b++) {
>             arr[a].nodelinks[b]=parseInt(links[b]);
>          }
>       } 
>    }
>}

I'm not at all familiar with the FileReader functions, but if you have
to create a function to handle an onload event, then I'm guessing that
readAsText must be asynchronous. So, what it does is *start* the read,
and then returns immediately. You then try and do something with mytext
but nothing has yet been put in it. So you get an error. Later, when
the read is complete, the onload fires and puts data into mytext.

When you run the second time, mytext contains the result of the first
load so things seem to work.

You need to put all the code to do something with mytext into the
onload function.

-- 
"Once you adopt the unix paradigm, the variants cease to be a problem - you
bitch, of course, but that's because bitching is fun, unlike M$ OS's, where 
bitching is required to keep your head from exploding." - S Stremler in afc

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.javascript


csiph-web