2017-06-23 21 views
0
//Blinking of the letter " X " on the Screen 
function blink() { 
    document.getElementById('blinkText').innerHTML = ""; 
    setTimeout("appear()", Math.random() * 3000); 
} 

//Re-Appearing of the letter " X " on the Screen 
function appear() { 
    document.getElementById('blinkText').innerHTML = "X"; 
    setTimeout("blink()", Math.random() * 3000); 
    return true; 
} 

// Counts the number of times a space bar is hit 
var hit = 0; 
window.onload = function hitSpace() { 
    document.body.onkeyup = function(e) { 
    if (e.keyCode == 32) { 
     hit = hit + 1; 
     document.getElementById("count").innerHTML = hit; 
    } 
    } 
    blink(); 
} 

//To run function hitSpace() only when the letter "X" appears, else display 
alert 

function checkForBlink() { 
    if (appear() == true) { 
    hitSpace(); 
    } else { 
    alert("You are too slow!") 
    } 
} 

基本的に私は何もできません。画面に文字Xが表示されたら、関数hitSpace()を使用してカウントしたい場合、 Xは表示されません。警告メッセージを表示します。私の関数checkForBlink()を修正するのを手伝ってくださいIf関数内に出現するIf文()If内に出現するIf文

+0

なぜあなたは 'hitspace()'を呼び出そうとしていますか?これは、ページがロードされたときに既に完了していたイベントリスナーを追加することだけです。 – Barmar

+0

Xが点滅しているときだけ、スペースバーのヒット数をカウントするにはどうすればよいですか? Xが点滅せず、スペースバーがヒットした場合は、警告メッセージを表示することでした – anonymous

答えて

1

hitSpace()に電話する必要はありません。 onkeyup機能にXがあるかどうか確認してください。

window.onload = function() { 
    document.body.onkeyup = function(e) { 
    if (e.keyCode == 32) { 
     if (document.getElementById("blinkText").innerHTML == "X") { 
     hit = hit + 1; 
     document.getElementById("count").innerHTML = hit; 
     } else { 
     alert("Sorry, you're too slow"); 
     } 
    } 
    } 
    blink(); 
}