I'm working on a chatbox feature using socket.io and I'm interested in adding a delay between message sending to prevent spam. Can someone provide guidance on how to achieve this?
Below is the code snippet for sending a message in my project:
// Function to send a chat message
const sendMessage = () => {
let message = $inputMessage.val();
// Cleanse input to prevent markup injection
message = cleanInput(message);
// Check if there is a non-empty message and an active socket connection
if (message && connected) {
$inputMessage.val("");
addChatMessage({ username, message });
// Instruct server to handle 'new message' event and pass one parameter
socket.emit("new message", message);
}
};