1


So I am getting an array from php using ajax. In ajax, I want to break down the array to put certain parts into certain divs.
I made code here which selects which parts fit where and put them there:

while (array[x] != null) {
  y = 0;
  if (2 < x) {
    if (x == 3) {
      x = 0;
      y = x;
    } else {
      y = x / 2
    }
  }
  setTimeout(function() {
    if (y == 0 || x % 3 === 0) {
      var namestring = array[y];
      var namestring = array[y].replace('[', '');
      var namestring = namestring.replace('[', '');
      var namestring = namestring.replace('"', '');
      var namestring = namestring.replace('"', '');
    }
    if (y % 2 != 0 || y % 3 != 0 && x > 0) {
      alert(y);
      var date = array[y]
      var date = date.replace('"', '');
      var date = date.replace('"', '');
    }
    if (x % 2 == 0 && x > 0) {
      var text = array[y];
      var text = text.replace('"', '');
      var text = text.replace('"', '');
      var text = text.replace("]", '');
      var text = text.replace("]", '');

      createcard(namestring, date, text);
    }
  }, 500);
  if (x > 500) {
    break;
  };
  x = x + 1;
}

The problem is that this part:

if(2 < x) {
    if (x == 3) {
        x = 0;
        y = x;
        } else {
            y = x/2
    }
}

causes the program to crash. If I do not have it, then it works fine. Also, if I do not have this then my problem doesn't work. Why is this happening?
Thanks!

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Tino Caer
  • 443
  • 4
  • 19
  • 1
    Take a look at [javascript closure inside loops](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example), while it might not be the cause of your current problem it is going to be a problem at some point – Patrick Evans Apr 19 '16 at 23:29
  • You can check by alerting after every line of ur if condition block that which line is creating the issue. – Aparna Apr 20 '16 at 01:08

1 Answers1

0

I solved it!

while (array[y] != null) {
                if (y == 0 || y % 3 === 0) {
                    alert(y);
                    var namestring = array[y];
                    alert(namestring);
                    var namestring = namestring.replace('[','');
                    var namestring = namestring.replace('[','');
                    var namestring= namestring.replace('"', '');
                    var namestring= namestring.replace('"', '');
                    y++;    
                }
                if (y % 2 != 0 || y % 3 != 0 && x > 0) {
                    var date = array[y]
                    var date = date.replace('"', '');
                    var date = date.replace('"', '');
                    y++;
                }
                if (y % 2 == 0 && y > 0) {
                    var text = array[y];
                    var text = text.replace('"', '');
                    var text = text.replace('"', '');
                    var text = text.replace("]", '');
                    var text = text.replace("]", '');
                    y++;

                    createcard(namestring,date,text);
                }
            if (y > 500) {
                break;
            };      
        }
Tino Caer
  • 443
  • 4
  • 19