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

90
Visualizações
Play audio once when div is visible

using jQuery 3.4.1 - I have set up a system where an administrator when logged in will have a script running every 5 seconds to check if any users have initiated a chat request. If so a window will appear with the users name and their initial question/message.

As well as the window I want to play an alert sound to let the admin know when a chat request is made but the issue is if I put the audio inside the script which checks for chat requests it will play immediately (if they log in and there's already a chat request) and then every 5 seconds which is very irritating.

To remedy I tried checking if the chat request div is visible then play the sound but it won't play at all this way and I'm not getting any console messages about it - I think this is happening because the sound requires user interaction in order to play.

Here's my current code.

 // Chat Request Notification Sound (not working)
 var chatstartsound = new Audio('../img/root/start.wav');
 if ($('#chatrequest').is(":visible"))
 { chatstartsound.play(); }

 // Chat Request
 (function chatrequest() {
 $.ajax({
 url:"../inc/chat-request.php",
 method:"POST",
 context: document.body, // This is to stop the AJAX spinner appearing every 5 seconds.
 global: false, // This is to stop the AJAX spinner appearing every 5 seconds.
 success:function(data) {
 $("#chatrequest").html(data);
 $("#chatrequest").delay(5000).show(200);
 }
 }).then(function() {
 setTimeout(chatrequest, 5000);
 });
 })();
about 4 years ago · Juan Pablo Isaza
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