作者XXZERO12 (Symphonia)
看板Ajax
标题[问题] 网页没有更新
时间Mon May 21 22:13:04 2012
要乱数取六个数字,我想设一个按钮能让他们一直跑。
我的想法如下:
先在一个大的div内印出六个span;
接着再用getElementByTagName搭配setInterval每0.5秒取代原本的数字;
先在没有按钮的情况下用while测试;
但是似乎没有反应;
不太清楚原因为何?
恳请解释!
程式码如如下:
function computeRandom(){
var value = new Array(49);//declare array
for ( i = 0; i < 49; i++ )//initial array_value
value[i] = i+1;
for ( i = 0 ;i < 100; i++ ) {//random arrange
x = parseInt(Math.random()*49);
y = parseInt(Math.random()*49);
tmp = value[x];
value[x] = value[y];
value[y] = tmp;
}
var color = new Array(49);
for ( i = 0; i < 49; i++ )//store color
color[i] = "rgb(" + parseInt(Math.random()*255) + "," +
parseInt(Math.random()*255) + ","
+ parseInt(Math.random()*255) + ")";
var value_tmp = new Array(6);
for( i = 0; i < 6; i++)
value_tmp[i] = value[i];
document.write("<div style = \"text-align:center;\" >");
//center the text by div
for( i = 0; i < 6; i++)
document.write("<span style = \"font-size: 2.5em; display:inline-block;
text-align:center;
width: 1.5em;
background: white;
color: " + color[i] + " \" > " + value_tmp[i]
+ "</span>     ");
var spanArray = document.getElementsByTagName("span");
setInterval("keepMove(value,spanArray)",10);
}
function keepMove(val,sp){
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];
document.write(sp[i].innerHTML+" ");
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.249.13
1F:→ dinos:宾果? 05/21 23:43
2F:→ XXZERO12:解决了 when you pass a string as the first parameter 05/22 01:07
3F:→ XXZERO12: of setInterval, it's like eval that string, and the 05/22 01:08
4F:→ XXZERO12:code will run in global environment 05/22 01:08