4

I've been testing some code which takes a variable in a json format and should print that, it prints an empty array however.

If I'm trying this:

console.log(JSON.stringify({first:1,second:2}));

Then I'm crashing the page (Chrome: "Aw, Snap!").

I've asked a few people, and they weren't able to reproduce it, I however get it every time. Tested it in FireFox too, and there it crashes too.

This was the code:

var timer={first:0,second:0,third:0,fourth:0};
localStorage.setItem('saveTimers', JSON.stringify(timer));

And that sets [] in the localStorage

Thomas
  • 649
  • 9
  • 17
  • can you paste the code?? – Zaffar Saffee Feb 05 '13 at 22:23
  • 1
    The code you posted does not crash for me. If we are not able to reproduce it, it's not really possible for us to help you solve the problem. – Felix Kling Feb 05 '13 at 22:23
  • 1
    works for me in Chrome 24.0.1312.57 m – Steve H. Feb 05 '13 at 22:24
  • Try {"first":1,"second":2} – Igor Jerosimić Feb 05 '13 at 22:28
  • Doesn't work either, and that's why I thought I should ask it here, maybe someone had it already, but so far no one seems to get that problem. I tested in Chrome 24.0.1312.57 aswell, and in FireFox 18.0.1 – Thomas Feb 05 '13 at 22:35
  • So, just to make sure. When you open this link: http://jsfiddle.net/fkling/cpNZt/, your browser crashes? – Felix Kling Feb 05 '13 at 22:37
  • Make sure nothing on your page is replacing the native JSON module (maybe an extension or some JS on the page you're on). – ozk Feb 05 '13 at 22:37
  • @FelixKling, That worked so far, if I'm pasting it in some .js script and run it, it crashes the page. It's originally stored in a var, and when I do this: localStorage.setItem('saveTimers', JSON.stringify(timer)); it returns []. – Thomas Feb 05 '13 at 22:43
  • And where/when does it crash? – Felix Kling Feb 05 '13 at 22:47
  • @FelixKling when executing the log, if I try to log(timer), it outputs []. With timer as stated in my question. – Thomas Feb 05 '13 at 22:55
  • 1
    But... if it outputs something, then it does not crash... I'm confused now. Please provide a http://jsfiddle.net/ demo that replicates your problem, this is too much guessing. – Felix Kling Feb 05 '13 at 22:56
  • I don't seem to be able to recreate it in jsfiddle, I'll have another word with the developper, he claims it works for him, I'll ask him to run some more tests with other people, some friends of mine also had this problem now. If I do get any answers concerning this I'll post them and edit the topic. – Thomas Feb 05 '13 at 23:18

3 Answers3

1

I was able to crash it when running it this a lot:

for (var i = 0; i < 100000; i ++) {
    var timer={first:0,second:0,third:0,fourth:0};
    localStorage.setItem('saveTimers', JSON.stringify(timer));
}

Perhaps you're running this code a bunch of times really quickly? A solution to this might be to throttle your function, which can be done by implementing a throttle function or with Underscore.js's throttle.

Evan Hahn
  • 12,147
  • 9
  • 41
  • 59
0

So by my understanding the problem you're really having is saving things to localStorage.

This seems more like a permissions problem than anything else. Does the browser have permission to access the filesystem? Are the folders used by the browser available for writing? Have you tried re-installing the browsers?

These are the things you should be checking.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

If you are using a beta or dev build of chrome, double check that this behaviour is the same in a stable build and if not, then there's your problem.

danp
  • 14,876
  • 6
  • 42
  • 48