0

I've trimmed the code down to what I think is relevant. This function is run once at the start of the $(document).ready function.

var evenGaps = [];
const NUMBERED_NEIGHBORS = //some array
var evenLocations = //some array
var gapNumber = 0;

function gapFinder() {
  for (var x = 0; x < NUMBERED_NEIGHBORS[evenLocations[i + 2]][1].length; x++) {
    if (NUMBERED_NEIGHBORS[evenLocations[i]][1][p] === 
      NUMBERED_NEIGHBORS[evenLocations[i + 2]][1][x]) {

      var delta0 = evenGaps[gapNumber]["1_Gap"][0] - evenGaps[gapNumber]["2_Filler"][0];
      var delta1 = evenGaps[gapNumber]["1_Gap"][1] - evenGaps[gapNumber]["2_Filler"][1];
      var delta = [delta0, delta1];

      evenGaps.push([]);
      evenGaps[gapNumber]["5_Distance"] = Math.max(Math.abs(delta[0]),
      Math.abs(delta[1]));

      if (Math.abs(delta[0]) === Math.abs(delta[1])) {
        evenGaps[gapNumber]["6_Single Path?"] = true;
      } else {
        evenGaps[gapNumber]["6_Single Path?"] = false;
      };

      if (evenGaps[gapNumber]["6_Single Path?"] && 
        evenGaps[gapNumber]["5_Distance"] >= 2) {

        //THE ISSUE ARISES WHEN THE LINE BELOW IS ADDED
        for (var i = 0; i < evenGaps[gapNumber]["5_Distance"]; i++) {}
      }
      gapNumber++;
    }
  }
}

I'm able to run the code without issue in Chrome when it doesn't include that last for() loop. Even if I run it as an empty loop, an infinite loop still occurs, however, it's the outer loop that's refusing to break (I know this because I have information printint to the console that's above the inner loop). This generally only occurs when I reload the page quickly. If I remove that one line of code, I can reload it as quickly and as many times as I want without issue. When I try running it in Firefox, it appears to also go into the loop but Firefox stops it with the stop script dialog box, however it shows the bad script as the for() loop that I'm saying is causing the issue.

Also, I'm using a very simple Node.js server.

1
  • Where does the i variable in the NUMBERED_NEIGHBORS[evenLocations[i + 2]][1].length expression come from? Commented Apr 21, 2016 at 3:58

1 Answer 1

1

You're using the variable i in this loop:

for (var x = 0; x < NUMBERED_NEIGHBORS[evenLocations[i + 2]][1].length; x++) {

What value it is taking depends on the rest of your code; perhaps it's a global variable; perhaps it's completely undefined - but your inner loop is then changing the value, affecting how the outer loop works.

Sign up to request clarification or add additional context in comments.

1 Comment

Going to hide my head in shame now. I didn't mention this, but the code above is wrapped in two other outer loops, one of which uses "i" as its counter, which you inferred (nice work). I changed the counter variable on the line that was giving me an issue and it appears to work now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.