作者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