JavaScript setInterval loop not holding variable
Posted
by Ryan
on Stack Overflow
See other posts from Stack Overflow
or by Ryan
Published on 2010-03-14T04:33:21Z
Indexed on
2010/03/14
5:05 UTC
Read the original article
Hit count: 317
JavaScript
|setinterval
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.
© Stack Overflow or respective owner