ユーザーが特定の期間内に特定の文字セットを入力したかどうかを判断しようとしています。
動作するものを作成したと思いますが、グローバル変数 toMatch を使用しているため、あまり良くないことはわかっています。var
キーワードなしで setInterval で宣言しました。スコープの考え方は私にとって混乱を招きますが、私は学ぼうとしています.誰かがこれを行うためのより良い方法を提供できるのではないでしょうか?
//set the toMatch array equal to the character codes for the word 'test'
//reset it to its original value every 2seconds
var matchTime = setInterval(function(){ console.log('toMatch reset'); toMatch = [84,69, 83, 84];}, 2000);
document.addEventListener("keydown", function(e){
var key = e.which;
findMatches(key);
});
function findMatches(key){
//if the key is in the first position in the array, remove it
if (key == toMatch[0]){
toMatch.shift();
}
console.log(toMatch);
//if all shifted out, clear the interval
if (toMatch.length == 0 ) {
window.clearInterval(matchTime);
alert('typed \'test\' within two seconds');
}
}
ありがとうございました