[問題] 網頁沒有更新

看板Ajax作者 (Symphonia)時間13年前 (2012/05/21 22:13), 編輯推噓0(004)
留言4則, 2人參與, 最新討論串1/2 (看更多)
要亂數取六個數字,我想設一個按鈕能讓他們一直跑。 我的想法如下: 先在一個大的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>&nbsp&nbsp&nbsp&nbsp&nbsp"); 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

05/21 23:43, , 1F
賓果?
05/21 23:43, 1F

05/22 01:07, , 2F
解決了 when you pass a string as the first parameter
05/22 01:07, 2F

05/22 01:08, , 3F
of setInterval, it's like eval that string, and the
05/22 01:08, 3F

05/22 01:08, , 4F
code will run in global environment
05/22 01:08, 4F
文章代碼(AID): #1FkaroTx (Ajax)
文章代碼(AID): #1FkaroTx (Ajax)