Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typing stop event to clear the feedback #20

Open
wants to merge 1 commit into
base: lesson-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ io.on('connection', (socket) => {
socket.broadcast.emit('typing', data);
});

// Handle typing-stop event
socket.on('typing-stop', function(data){
socket.broadcast.emit('typing-stop', data);
});

});
18 changes: 17 additions & 1 deletion public/chat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Make connection
var socket = io.connect('http://localhost:4000');

let timer,
timeoutVal = 1000; // time it takes to wait for user to stop typing in ms

// Query DOM
var message = document.getElementById('message'),
handle = document.getElementById('handle'),
Expand All @@ -18,8 +21,17 @@ btn.addEventListener('click', function(){
});

message.addEventListener('keypress', function(){
window.clearTimeout(timer);
socket.emit('typing', handle.value);
})
});

message.addEventListener('keyup', function(){
window.clearTimeout(timer); // prevent errant multiple timeouts from being generated
timer = window.setTimeout(() => {
socket.emit('typing-stop', handle.value);
}, timeoutVal);

});

// Listen for events
socket.on('chat', function(data){
Expand All @@ -30,3 +42,7 @@ socket.on('chat', function(data){
socket.on('typing', function(data){
feedback.innerHTML = '<p><em>' + data + ' is typing a message...</em></p>';
});

socket.on('typing-stop', function(data){
feedback.innerHTML = '';
});