0
我試圖編寫一個遊戲,當單擊該按鈕時調用外部.js腳本。代碼以我想要的方式工作,但我對如何將輸出轉換爲HTML以使其可以正確格式化有點困惑。按鈕應該在點擊時重新啓動進程(即不必重新加載頁面),以便進行新的隨機選擇。從外部腳本調用ID的按鈕
我終究要還包括一個按鈕來刪除最後顯示的項目,但這是以後....
的Javascript:
var basket = ['apple', 'banana', 'cherry', 'durian', 'eggplant', 'fig', 'grapes', 'huckleberry', 'kiwi', 'lemon', 'mango'];
function randOrd(){
return (Math.round(Math.random())-0.5); }
mixedBasket = basket.sort(randOrd); //randomize the array
var i = 0; // the index of the current item to show
fruitDisplay = setInterval(function() {
document
.getElementById('fruit')
.innerHTML = mixedBasket[i++]; // get the item and increment
if (i == mixedBasket.length) i = 0; // reset to first element if you've reached the end
}, 70); //speed to display items
var endFruitDisplay = setTimeout(function() { clearInterval(fruitDisplay); }, 3000);
//stop display after x milliseconds
當前的HTML顯然運行的代碼,但它在按鈕被點擊之前開始。
HTML:
<head><script type="text/javascript" src="Fruitpicker3.js"></script></head>
<body>
<span id = "fruit"> </span>
<input type="button" onclick="fruit" value="Start"/>
</body>
</html>
預先感謝您的幫助!
謝謝!兩天來,我一直在抨擊我的頭。 – Tyler330
如果這回答了您的問題,請將其標記爲正確(左側的空白複選標記,謝謝! –