2016-12-17 7 views
1

これは本当にばかげた質問ですが、私は外国のアルファベットを学ぶための簡単なアプリを作ろうとしています。ボタンをクリックして値をリセットするコードが必要なので、ページをリロードする必要はありません。Javascript異なるボタンをクリックして作業するネストされた関数?

ボタンをクリックして音を聞いて文字をクリックし、それがサウンドに対応しているかどうかを確認し、そうであれば "bravo"メッセージを書き込み、処理を続行することです。

私は、割り当てられたサウンドをランダムにしたいので「ボタンのクリックでプロセスをリセットする」以外はすべて行いました。グローバル変数にすると、クリック時にリセットできません「次の」ボタンを押しても、それを関数に入れると、ユーザーが別の関数で正しい文字を選択したかどうかを確認するためにその値を使用することはできません。

How it looks

私のhtml:

<button type="button" onclick="dodeli()">Assign</button> <!--Assigns the random value--> 
<input type="text" id="upisi"> <!--Text field to write the answer in--> 
<button class="soundBt" onclick="zadaj()">Sound</button> <!-- Plays the sound of a letter --> 
<button class="soundBt" onclick="slovoA()">A</button> <!-- Writes the letter "A" and plays its sound--> 
<button class="soundBt" onclick="slovoB()">Б</button> <!-- Writes the letter "B" and plays its sound --> 
<button class="soundBt" onclick="proveri()">Check</button><!-- Checks if it's the correct letter --> 
<button class="soundBt" onclick="dalje()">Next</button><!-- Starts the cicle again --> 
<audio id="audio" src=""></audio><!-- Audio of a letter when "sound" is clicked --> 
<audio id="audioSlova" src=""><!-- Audio of a letter when the specific letter is clicked --> 

私のjavascript:

//variables that I need to reset on click of the "next" button 

var array = ["a.ogv", "b.ogv", "v.ogv"]; 
var random =Math.floor(Math.random() * 3); 
var rand = array[random]; 
var sound = document.getElementById('audio'); 
var text = document.getElementById("upisi"); 

//function that ties the random source to the audio 

function dodeli(){ 
    sound.src = rand; 
} 
    //function that plays the sound of a letter that user needs to guess 

    function zadaj(){ 
    sound.play(); 
    } 

//plays the sound of a clicked letter and writes it into the text field 

var soundLetter = document.getElementById('audioSlova'); 
function slovoA(){ 
    soundLetter.src = "a.ogv" 
    soundLetter.play(); 
    text.value = "a.ogv"; 
} 

function slovoB(){ 
    soundLetter.src = "b.ogv" 
    soundLetter.play(); 
    text.value = "b.ogv"; 
} 
//checks if it is the same as the assigned value 

function proveri(){ 
    if(text.value == rand){ 
    alert("bravo"); 
    }else{ 
    alert("try again!"); 
    } 
} 

私は私が考えることができるすべてのものを試してみました。私が "次へ"ボタンのクリックでリセットする必要がある値を設定する関数で "return"を使用しようとすると、呼び出されるたびにリセットされ、グローバル変数を避けるためにそのモジュールを使用しようとしました(私は名前を今思い出すことができません)しかし、それは同じ問題です。

p.s.私はいくつかの変数名を英語に変更して読みやすくしました。何かが間違っているように見えるかもしれませんが、私のコードはうまく動作しますが、唯一の問題はクリックして読み込む方法がわからないことですページ全体をリロードする必要はありません。

答えて

0

1つの配列を使用してすべてのMP3を保持し、別の配列を回答キーとして使用します。次に、両方の配列を同時にシャッフルして、ある配列の各要素が他の配列のインデックス番号と一致するようにします。大きな利点は、両方のアレイが事前シャッフルであるため、何もリセットする必要がないことです。

"唯一の問題は、ボタンをクリックしてリロードしてページ全体をリロードする必要がないことです。"

私はオーディオ.srcプロパティが変更されたとき、それがリロードされるまで、それが再生されませんので、ページを再ロードする必要がある理由はあると仮定します。 .srcを変更した後にページを再ロードしないようにするには、​​プロパティを使用します。

次のスニペットには、ソースでコメントされている詳細があります。ところで、サウンドボタンをクリックしてMP3を再生すると、遅延が発生することがあります。 MP3はGoogleドライブでホストされています。

SNIPPET

/*// = For debugging purposes */
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width,initial-scale=1, user-scalable=no"> 
 
    <title>Phonetic Test</title> 
 
    <link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet'> 
 
    <link href="https://cdn.jsdelivr.net/fontawesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"> 
 
    <style> 
 
    details { 
 
     margin: 50px auto; 
 
     cursor: pointer; 
 
    } 
 
    summary { 
 
     font-size: large; 
 
     font-variant: small-caps; 
 
     outline: 0 none transparent; 
 
    } 
 
    .fa { 
 
     font-size: 18px; 
 
    } 
 
    #answer { 
 
     line-height: 22px; 
 
     font-size: 16px; 
 
     font-weight: 900; 
 
     padding: 4px; 
 
     width: 4ex; 
 
     cursor: text; 
 
     vertical-align: bottom; 
 
    } 
 
    </style> 
 
</head> 
 

 
<body> 
 
    <header> 
 
    </header> 
 
    <main class='container'> 
 
    <section class='row'> 
 
     <details class='col-md-6'> 
 
     <summary class='media-heading'>Phonetic Test</summary> 
 
     <fieldset class='form-group-sm'> 
 
      <button class="btn btn-primary" id='clip'><i class='fa fa-play-circle'></i>&nbsp;Sound</button> 
 
      <input class='input' type="text" id="answer"> 
 
      <button class="btn btn-info" id='check'><i class='fa fa-check-circle'></i>&nbsp;Check</button> 
 
      <button class='btn btn-success hide' id='next' onclick="nextLetter()">Next&nbsp;<i class='fa fa-arrow-circle-right'></i> 
 
      </button> 
 
     </fieldset> 
 
     </details> 
 
     <audio id="audio" src="https://www-drv.com/site/kp6h65kzzsawpdclyfbbkq/pub/am/test.mp3"></audio> 
 
     <output id='display'></output> 
 
     <!-- --> 
 
    </section> 
 
    </main> 
 
    <footer> 
 
    </footer> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
 
    <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js'></script> 
 
    <script> 
 
    // String of url base 
 
    var root = 'https://www-drv.com/site/kp6h65kzzsawpdclyfbbkq/pub/am/letters/'; 
 
    // Array of mp3 
 
    var list = ["a.mp3", "b.mp3", "c.mp3", "d.mp3", "e.mp3", "f.mp3", "g.mp3", "h.mp3", "i.mp3", "j.mp3", "k.mp3", "l.mp3", "m.mp3", "n.mp3", "o.mp3", "p.mp3", "q.mp3", "r.mp3", "s.mp3", "t.mp3", "u.mp3", "v.mp3", "w.mp3", "x.mp3", "y.mp3", "z.mp3"]; 
 
    // Array of the alphabet 
 
    var answ = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; 
 
    // Number index of current turn 
 
    var turn = 0; 
 
    // Reference the <audio> 
 
    var player = document.getElementById("audio"); 
 
    // Reference the text <input> 
 
    var text = document.getElementById("answer"); 
 
    // Reference the text <output> // 
 
    var display = document.getElementById('display'); 
 
    // Listen for click event and invoke playAudio() 
 
    document.getElementById('clip').addEventListener('click', playAudio); 
 
    // Listen for click event and invoke checkAnswer() 
 
    document.getElementById('check').addEventListener('click', checkAnswer); 
 
    // Listen for click event and invoke nextLetter() 
 
    document.getElementById('next').addEventListener('click', nextLetter); 
 

 
    function playAudio() { 
 

 
     if (player.src === "https://www-drv.com/site/kp6h65kzzsawpdclyfbbkq/pub/am/test.mp3") { 
 
     // "path" is the url that points to the current mp3 
 
     var path = root + list[turn]; 
 
     // Assign the "path" to .src prroperty 
 
     player.src = path; 
 
     /* console.log(path); */ // 
 

 
     /* Whenever an <audio> element's .src has changed, 
 
\t \t | use the .load() property afterwards 
 
\t \t */ 
 
     player.load(); 
 
     player.play(); 
 
     } else { 
 
     player.play(); 
 
     } 
 
    } 
 

 
    function checkAnswer() { 
 
     /* console.log(answ[turn]); */ // 
 

 
     /* Compare value of answer to the current index 
 
\t | of the answ[] array 
 
\t */ 
 
     if (text.value === answ[turn]) { 
 

 
     // Reveal the #next button 
 
     next.classList.remove('hide'); 
 
     alert("Bravo!"); 
 

 
     // Increment the 'turn' counter 
 
     turn++; 
 
     } else { 
 
     alert("Try again!"); 
 
     } 
 
    } 
 

 
    function nextLetter() { 
 
     /* console.log(turn); */ // 
 
     player.src = "https://www-drv.com/site/kp6h65kzzsawpdclyfbbkq/pub/am/test.mp3"; 
 
     // Clear text <input> 
 
     text.value = ""; 
 
     // Hide the #next button 
 
     next.classList.add('hide'); 
 
    } 
 
    
 
    // Shuffle 2 arrays simultaneously 
 
    function shuffle(array1, array2) { 
 
     var x = 0; 
 
     var y = 0; 
 
     var t1 = null; 
 
     var t2 = null; 
 

 
     for (x = array1.length - 1; x > 0; x -= 1) { 
 
     y = Math.floor(Math.random() * (x + 1)); 
 
     t1 = array1[x]; 
 
     t2 = array2[x]; 
 
     array1[x] = array1[y]; 
 
     array2[x] = array2[y]; 
 
     array1[y] = t1; 
 
     array2[y] = t2; 
 
     } 
 
    } 
 

 
    /* On window load invoke shuffle() 
 
    | passing the list[] and answ[] arrays 
 
    */ 
 
    window.onload = function() { 
 
     shuffle(list, answ); 
 
     /* console.log(list); */ // 
 
     /* console.log(answ); */ // 
 
     /* display.textContent = answ; */ // 
 
    } 
 
    </script> 
 
</body> 
 

 
</html>

+0

私は数日のためにインターネット接続を持っていなかったので、ありがとうございました!それは素晴らしい、そして詳細な答えです。私が少し問題を抱えているのは、 "turn"変数の増分だけです。私は毎回違う価値を得ることができると思っていますよね?だから配列 "list"と "answ"に移動しますか?それが終わりに達するとどうなりますか?それとも、大きな時間を増やしているのか分かりませんか? – ProgrammingNewbie

+0

* "..."ターン変数をインクリメントするとき、毎回異なる値を得るようになっていると思いますよね? "* – zer00ne

+0

*" ... "ターン変数のインクリメント;私たちが毎回違う価値を得られるように推測していますよね? " * "それで配列" list "と" answ "を移動しますか?" *>肯定的です。 * "それが終わりになると何が起こるのですか?" * 26で停止するのを忘れて、限界を追加して、更新を通知します* "またはそれのポイントが足りなくなっていますか?その要約は正確で正確ですが、これで問題が解決しない場合は私に通知してください。すぐに26にキャップが更新され、ATMが働いています。 – zer00ne

関連する問題