views:

239

answers:

4

Here is my code:

    var showNo = 1;     
    window.setInterval(function() {
          console.log(showNo);
          if(showNo === 1) { var nextNo = 2;  }
          else if(showNo === 2) { var nextNo = 3;  }
          else if(showNo === 3) { var nextNo = 4;  }
          else if(showNo === 4) { var nextNo = 5;  }
          else if(showNo === 5) { var nextNo = 1;  }
          else { var showNo = 1; var nextNo = 2; }

          var showNo = nextNo;
          }, 500);

My question is, why is the showNo variable not holding when the setInterval loop starts? The console displays 'undefined' in the above example. This may be a simple question, but I am trying to teach myself query and this has me stuck..

Any answers would be great.

Thanks.

+4  A: 

You are re-creating a new LOCAL variable called showNo, this is not referencing the GLOBAL variable called showNo.

It is very bad practice to use global variables, I advise wrapping this inside of an anonymous function

I think this is what you're looking to do:

  (function() {
      var showNo = 1;     
      window.setInterval(function() {
            console.log(showNo);

            if( showNo >== 1 && showNo <== 4 ) {  
                showNo++;
            } else if( showNo === 5 ) {
                showNo = 1;  
            } else { 
                showNo = 2; 
            }

            }, 500);
    })();
Jacob Relkin
is there a reason for that? taking it out does not allow the showNo variable to be remembered..
Ryan
ahh yes I see now. So I must first create the var, the go ahead and set it, not recreate it every time? That works, thanks alot.
Ryan
A: 

what @Jacob said is true, but you might want to look at simplifying your code a little like this:

var showNo = 1;     
window.setInterval(function() {
      console.log(showNo);
      showNo++;
      if(showNo > 4)
          showNo = 1;
      }, 500);
John Boker
@John, what will happen if `showNo` is `0`?
Jacob Relkin
According to the OP's logic, it should be `2`.
Jacob Relkin
Yeah, I ended up using jquery size() to count the elements that it needed, and going if(previewCurrent === previewTotal) { previewNext = 1; }else { previewNext = previewCurrent+1; }Thanks
Ryan
A: 

I would recommend you to read Javascript Closures, then you'll have a deep understanding about how identifiers are resolved in JavaScript.

Alex Cheng
This is not an issue of closure, it's an issue of re-declaring a variable in the new scope.
Justin Johnson
A: 

In the wide world of JavaScript, var declarations are function scoped, not block scoped. They are also elevated to the top of a function. So, you might as well have written:

var showNo = 1;     
    window.setInterval(function() {
          var showNo; // I'm localizing it
          var showNo; // and again
          var nextNo; // Now I'm declaring a new variable
          var nextNo; // and I just can't stop
          var nextNo; // myself from declaring it again
          var nextNo; // its like beating
          var nextNo; // a
          var nextNo; // dead horse.
          console.log(showNo);
          if(showNo === 1) { nextNo = 2;  }
          else if(showNo === 2) { nextNo = 3;  }
          else if(showNo === 3) { nextNo = 4;  }
          else if(showNo === 4) { nextNo = 5;  }
          else if(showNo === 5) {  nextNo = 1;  }
          else { showNo = 1; nextNo = 2; }

          showNo = nextNo;
          }, 500);

You can probably see the problem now.

Everyone else's advise on refactoring this is important as well. But understand scoping in javaScript, and these annoyances will disappear.

davehamptonusa
@davehamptonusa ;)
Jacob Relkin