0

I am having the strangest problem with javascript objects. I have a function that is an event handler that looks like this:

.on(some-event, function(event){
    console.log(event);
    console.log(event.bytesLoaded);
    console.log(event.duration);
    console.log(JSON.stringify(event));
})

The chrome debuggger shows the first line as this:

bytesLoaded:4575118
bytesTotal:4575118
delayLoadingTime:1503675333159
duration:4.004

The second line as:

NaN

The third line as:

4.004

And the fourth line as:

{"bytesLoaded":null,"bytesTotal":null,"delayLoadingTime":1503675822834,"duration":4.004}

Does anybody know why this strange behavior is happening? Is the chrome debugger lying about seeing an actual number in that field? The duration field works fine in both the debugger and being accessed through javascript.

avern
  • 4,741
  • 3
  • 11
  • 16
  • Looks like your using jQuery, see what `console.log(event.originalEvent.bytesLoaded);` gives you – Joe Lissner Aug 25 '17 at 15:51
  • try logging `event.bytesLoaded.toString()` and see what happens – Amin Jafari Aug 25 '17 at 15:52
  • That is some very strange behavior but you don't want to do JSON.stringify on the event object. [This post](https://stackoverflow.com/questions/11547672/how-to-stringify-event-object) explains a lot more about it. Most likely what you want to do is just get the attributes off the event that you care about, put them in a new object and then serialize that object. – Joseph Ditton Aug 25 '17 at 15:54
  • @JoeLissner console.log(event.originalEvent); gives undefined – avern Aug 25 '17 at 15:55
  • @AminJafari console.log(event.bytesLoaded.toString()) gives NaN – avern Aug 25 '17 at 15:55
  • Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a *specific problem or error* and *the shortest code necessary* to reproduce it **in the question itself**. Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Andreas Aug 25 '17 at 16:01

0 Answers0