简体   繁体   中英

How to stop audio alert?

I have an alert box and an audio alert . When clicking on close image of alert box I want to stop the audio alert . I have done like this. Both alert box and Audio alarm comes properly. But Audio stops just after a beep(ie, before closing the alert message).

<script type="text/javascript" src="jquery-1.2.3.pack.js"></script>
<script type="text/javascript">
    $(document).ready(function()
    {
        //first slide down and blink the alert box
        $("#object").animate({ 
            top: "0px"
        }, 2000).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
        //stop the audio alarm
        //close the message box when the cross image is clicked 
        $("#close_message").click(function()
        {
            audio.pause();  //stop the audio alarm
            $("#object").fadeOut("slow");
        });
    });
</script>


if($a>$B)
{
    echo " <script> var audio = document.createElement('audio');
    document.body.appendChild(audio);
    audio.src = '/sounds-990-system-fault.mp3';
    audio.addEventListener('canplaythrough', function() { 
        audio.play(); 
    }, 5000);

    </script>";

    echo '<div id="object" class="message">' ;
    echo '<img id="close_message" style="float:right;cursor:pointer"  src="12-em-cross.png" />';
    echo '<strong>Failed </strong>';
    echo '</div>';
} 

Adding a loop attribute to your audio element should be enough. This way the sound will play indefintely until you actually pause it by code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM