执行函数shows()出现在If语句

时间:2017-06-23 01:04:19

标签: javascript html css web

//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()

1 个答案:

答案 0 :(得分:1)

您无需致电hitSpace()。检查X函数中是否有onkeyup

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();
}