1

時限イベントの結果を取り出せるようにしたいのですが、そのインデックスを見つける方法がわかりません。問題は、時間によって配列から項目を選択していることですが、インデックス番号によって選択しているため、タイマーが実行された後に結果を取得できる必要があります。

それはの線に沿ったものになります

mixedgroup1.splice(i, 1);

ここで、i は最終結果のインデックス番号ですが、取得方法がわかりません。

何か案は?

JSコードは次のとおりです。

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


 function showBasket(){
 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); }, 3600); 
 //stop display after x milliseconds
}

これはHTMLです:

<html>

<head></head>
<body>


<center>
   <h1> <span id="fruit"></span><p></h1>
 <script> var fruitSound = new Audio(); 
        fruitSound.src = "boardfill.mp3"; 

    function showFruitwithSound()
    { 
    fruitSound.play(); // Play button sound now 
    showBasket()
    } 

 </script>

<button onclick="showFruitwithSound()">Choose Fruit</button>

</center> 

<script type="text/javascript" src="Fruitpicker3.js"></script>
</body>
</html>
4

2 に答える 2

0

非常に密集して申し訳ありませんが、私はjsにかなり慣れていません。私はそれからまったく出力を得ることができません。書き換えは次のとおりです。

 <center>
 <h1> <span id="fruit"></span><p></h1> 

   <script> var fruitSound = new Audio(); 
    fruitSound.src = "boardfill.mp3"; 

    function showFruitwithSound()
    { 
    fruitSound.play(); // Play button sound now 
    showBasket()
    } 

 </script>

 <p id = "indexnum">
 <button onclick="showFruitwithSound()">Choose Fruit</button>

   </center> 
 <script>  
 var indexf = mixedBasket.indexOf(document.getElementById('fruit').innerHTML);
 document.getElementById("indexnum").innerHTML = indexf + " this is the index number."; 
 </script>

  <script type="text/javascript" src="Fruitpicker3.js"></script>
 </body>

于 2014-12-22T16:14:07.513 に答える