【发布时间】:2017-11-21 02:51:55
【问题描述】:
在我的 correctClick 函数中,我将按钮单击与一个数组 (buttonPressValidate) 进行比较,该数组 (buttonPressValidate) 包含屏幕上突出显示的所有按钮的 id。如果单击突出显示的按钮,则 replayFlash 函数会突出显示 buttonPressValidate 中的所有按钮,然后在 replayFlash 函数完成后 highLightSquare 函数应该高亮一个新的正方形。我遇到的问题是我的 highLightSquare 函数在突出显示新方块之前没有等待 replayFlash 函数完成。
var clickNum = 0;
function correctClick(buttons) {
$(".button").on("click", function() {
var thisClick = $(this).attr("id");
var matchBut = buttonPressValidate[clickNum];
if(thisClick == matchBut) {
clickNum++;
setTimeout(function() {
replayFlash(buttonPress);
if(buttonPressValidate.length === clickNum) {
setTimeout(function() {
highLightSquare(simonButtons);
}, 1500);
}
}, 1500);
}
});
}
function replayFlash(butPressArr) {
function eachColor(i) {
var litColor = $(butPressArr[i]);
setTimeout(function() {
litColor.addClass("lit");
if(litColor.attr("id") === "greenButton") {
greenButton.play();
} else if(litColor.attr("id") === "redButton") {
redButton.play();
} else if(litColor.attr("id") === "blueButton") {
blueButton.play();
} else if(litColor.attr("id") === "yellowButton") {
yellowButton.play();
}
setTimeout(function() {
litColor.removeClass("lit");
}, 1000 - (1000 / 3));
}, 1000 * (i + 1));
}
for(var i = 0; i < butPressArr.length; i++) {
eachColor(i);
}
}
【问题讨论】:
-
西蒙经常出现。这是一些编程课程的练习吗?
-
是的,它是 FreeCodeCamp 课程的最后一个高级项目。
-
你能告诉我们你的 replayFlash 功能吗?例如,如果它还调用 setTimeout,则代码将继续执行,因为 setTimeout 是异步的。
-
我在帖子中添加了replayFlash功能
-
课程是否包含 Promises?
标签: javascript jquery