Display notification div with JAVASCRIPT audio

Asked

Viewed 342 times

0

So, I made a code with jquery that after 8 seconds of open index page displays an audio message div.

$(document).ready(function () {
    // Mostra a div após 8 segundos
    setTimeout(function() {
        $('.informativo').show().append('<embed src="<?php echo $address ?>sound.mp3" autostart="true" hidden="true" loop="false">');
        // Esconde a div após 8 segundos
        setInterval(function () {
                $('.informativo').fadeOut();
        }, 8000);
    }, 4000);        
});    

<div class="informativo">
    <p>Mensagem de alerta com audio</p>
</div>

But I’m having a problem that if I open this index page and change tab, the div appears in the normal index tab, but the audio only runs when I go back to index!

I wanted the audio in the embed to actually run, me being in another tab!

  • You can post the complete code with the relative HTML so we can test?

  • In fact if the audio is already running and change tab it will continue running.

  • I posted the necessary HTML

  • @Leocaracciolo so the sound is really fast! after 8 seconds it appears with the div and if I open the page and change tab before 8 seconds, it does not play! It only rings when I return to the system tab

  • This, but if it is already running, it will continue running regardless of which page you are browsing since q vc does not close the audio tab

  • Guy really your code there is no mistake! Here mine does not work the way

  • Take the source code of the page

  • I just put the audio URL in your code

  • Strange too dude, I’ll try to clear my cache! To see if it helps anything!

  • which browser?

  • Chrome and updated!

  • Instead of using setTimeout(), Try replacing delay with delay: $('.informativo').show().apped('...').delay(8000);, this way you will shoot the waiting for the event right at the beginning, this setInterval() will be repeating itself eternally while the tab is open every 8 seconds, was that right? The audio will only stop if you close the tab.

Show 7 more comments

1 answer

0

The code is being used in PHP, would be index.php, configure a JS file that has PHP and call it in the index header;

 <embed src="<?php echo $address ?>sound.mp3" autostart="true" hidden="true" loop="false">

When you leave the index.php the other page can’t locate the music directory because you might have put the code only on one page.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.