처음에는 keyup
에 "타이핑 중"이라는 메시지를 보내고 window.setTimeout
을 사용하면 10 초 정도 후에 "타이핑하지 않음"메시지를 보냅니다. 모든 키 입력시마다 setTimeout을 취소하고 일정을 변경하십시오. 아래
테스트되지 않은 예 :
var input = document.getElementById('typeeBox');
var isTyping = false;
var timeoutId = 0;
function stoppedTyping() {
isTyping = false;
// Send not typing message.
// Update db status.
}
function startedTyping() {
if (!isTyping) {
// Send is typing message.
// Update db status.
isTyping = true;
timeoutId = window.setTimeout(stoppedTyping, 10000);
} else {
window.clearTimeout(timeoutId);
timeoutId = window.setTimeout(stoppedTyping, 10000);
}
}
input.addEventListener('keyup', startedTyping, false);
는'keydown' 핸들러를 사용합니다. 사용자가 텍스트 상자에 문자를 채우는 경우 "입력"메시지를 보내고 사용자가 텍스트 상자를 비우면 빈 메시지를 보내 (메시지 출력을 지우려면) –
고맙습니다.이 코드도 작성할 수 있는지 확인하려고합니다. – Ingrid