Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
How to add a delay between function excutions

I am creating this little chatbox thing with socket.io, and I am wondering how I can add a delay between message sending to prevent spam and such.

Here is my code for sending a message.

    // Sends a chat message
    const sendMessage = () => {
        let message = $inputMessage.val();
        // Prevent markup from being injected into the message
        message = cleanInput(message);
        // if there is a non-empty message and a socket connection
        if (message && connected) {
            $inputMessage.val("");
            addChatMessage({ username, message });
            // tell server to execute 'new message' and send along one parameter
            socket.emit("new message", message);
        }
    };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could add a variable that tracks the time of the most recently sent message and a comparison of this variable and the current time; if the difference is too small, refuse to send it.

Minimal example:

window.lastMessageTime = 0;
function sendMessage(message) {
  if(Date.now() - window.lastMessageTime < 5000)
    return false;
  window.lastMessageTime = Date.now();
  // Proceed
};
about 4 years ago · Juan Pablo Isaza Relatório

0

Try using setTimeout to execute your main function. Javascript is not supporting any delays as it is single threaded.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda