I want to make a button to start the onclick to make the random numbers keep running,(use
- or 2. to call) but if I press it , the error message shows that “Uncaught
ReferenceError: spanArray is not defined” but above the number is still running, I cannot
understand where is the wrong place.
PS But if I use 3 it work normally.
Thx!
thx complete code I try by 3 is http://jsfiddle.net/eVyjC/
function computeRandom(){
// skip code: have used loop to create six spans already
var spanArray = document.getElementsByTagName("span");
//1.document.write("<input type = button value = 'start' onclick = \"passKeepMove(value,spanArray) \" name = button1>");
//2.document.write("<input type = button value = 'start' onclick = \"setInterval(function(){keepMove(value,spanArray);}, 10) \" name = button1>");
//3.setInterval(function(){keepMove(value,spanArray);}, 10) ;
}
function keepMove(val,sp){//call by pointer
var index = parseInt(Math.random()*43);//set a increment to avoid repeatition
for( i = 0; i < sp.length; i++){
sp[i].innerHTML = val[i+index];
}
}
function passKeepMove(v,s){
setInterval(function(){keepMove(v, s);}, 10);
}
The
varin front ofvar spanArrayis makingspanArraya local variable. So you can not access it outside ofcomputeRandom‘s function scope.You need to make it global or come up with a better design so you do not need global variables.