我想制作一个按钮来启动 onclick 以使随机数继续运行,(使用
- 或 2. 打电话),但如果我按下它,错误消息会显示“未捕获
ReferenceError: spanArray is not defined" 但上面的数字仍在运行,我不能
明白哪里错了。
PS 但是如果我使用 3 它可以正常工作。
谢谢!
我尝试 3 的完整代码是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);
}