無法在 keyDown 或 keyPress 事件處理程式中呼叫函式。
var buttonColors = ["red", "blue", "green", "yellow"];
var gamePattern = [];
$(document).on("keydown", nextSequence);
function nextSequence() {
var randomNum = Math.floor(Math.random() * 4);
var randomChosenColor = buttonColors[randomNum];
return gamePattern.push(randomChosenColor);
}
uj5u.com熱心網友回復:
您可能不需要回傳,因為您的變數的范圍在回呼之外。正如@Samathingamajig 指出的那樣,它實際上正在作業。您也可以keydown使用以下方式呼叫速記版本
$(document).keydown(function () {
var randomNum = Math.floor(Math.random() * 4);
var randomChosenColor = buttonColors[randomNum];
gamePattern.push(randomChosenColor);
// Check the current state of gamePattern
console.log(gamePattern);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422572.html
標籤:
