사용자가 특정 시간 내에 특정 문자 집합을 입력했는지 확인하려고합니다.특정 시간 내에 특정 문자를 입력했는지 감지합니다.
나는 뭔가를 만들었다 고 생각 하긴하지만 전역 변수 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');
}
}
는
왜 'findMatches'에서 정의 할 수 없습니까? –
@Nicholas array.shift()를 사용하면 어떻게됩니까? – luckystars