我很确定这只是一个语法问题,但经过一堆谷歌搜索,我无法找到如何正确执行此操作。我试图在一个对象内部的函数内部使用setTimeout来调用同一对象内的不同函数。由于某种原因,该函数未被调用。这是相关的代码。有问题的setTimeout位于第一个函数hitKey中。在此先感谢您的帮助:
var gameBoard = {
greenKey: document.querySelector("#green"),
redKey: document.querySelector("#red"),
blueKey: document.querySelector("#blue"),
yellowKey: document.querySelector("#yellow"),
allKeys: document.querySelector("#keys"),
hitKey: function(keyColor) {
this.lightKey(keyColor);
setTimeout(this.unlightKey, 1000, keyColor);
},
enableKeys: function() {
this.allKeys.addEventListener("click, hitKey");
},
disableKeys: function() {
this.allKeys.removeEventListener("click, hitKey");
},
lightKey: function(keyColor) {
switch (keyColor) {
case "green":
this.greenKey.style.backgroundColor = "GreenYellow";
break;
case "red":
this.redKey.style.backgroundColor = "DeepPink";
break;
case "blue":
this.blueKey.style.backgroundColor = "Cyan";
break;
case "yellow":
this.yellowKey.style.backgroundColor = "Gold";
break;
}
},
unlightKey: function(keyColor) {
switch (keyColor) {
case "green":
this.greenKey.style.backgroundColor = "Green";
break;
case "red":
this.redKey.style.backgroundColor = "Red";
break;
case "blue":
this.blueKey.style.backgroundColor = "Blue";
break;
case "yellow":
this.yellowKey.style.backgroundColor = "Yellow";
break;
}
}
};