0

I have a code, and code work, but the sounds always running / play, I hope the sounds just once playing if there is a new data entry, but can not. I hope my friends can help me. thnk

       function notification(){

        $('<audio id="chatAudio"><source src="../sound/notify.ogg" type="audio/ogg"><source 
           src="../sound/notify.mp3" type="audio/mpeg"><source src="../sound/notify.wav" 
           type="audio/wav"></audio>').appendTo('body');

        $.ajax({
            url:"check.php",
            chace: false,
            success: function(data){
                if(data == 0){
                    $(".place").hide();
                }else{
                    $(".place").show("fast");
                    $(".place").html(data);
                    $('#chatAudio')[0].play();
                }
            }
        }).always(function(){
            setTimeout(function(){notification()}, 3000);       
        }); 
    }

$(document).ready(function(){
   notification();
});
evan
  • 12,307
  • 7
  • 37
  • 51
Whendy
  • 11
  • 4
  • you can use `.play()`, `.pause()` methods in javascript on sound object ie on `#chatAudio`. For more see : http://stackoverflow.com/questions/3566967/how-to-stop-audio-played-by-audio-tag-of-html5 – Vedant Terkar Jul 20 '14 at 18:29
  • @Vedant how to define – Whendy Jul 20 '14 at 18:55
  • Move the adding of the `audio` element out of the notification function as you'll end up appending it many times currently. – Rob Schmuecker Jul 20 '14 at 21:31

1 Answers1

0

Your code looks like it will work if you move the adding of the audio element to outside of the notification function so it only gets added once. I have refactored your code somewhat and it ought to work as intended. Just make sure that your PHP is returning 0 when there are no new notifications. Also I am unsure what chace means in your JS?

       function notification(){
        $.ajax({
            url:"check.php",
            chace: false,  //<-- Not sure what this is meant to be?!
            success: function(data){
                if(data == 0){
                    $(".place").hide();
                }else{
                    $(".place").show("fast");
                    $(".place").html(data);
                    $('#chatAudio')[0].play();
                }
            }
        }); 
    }

$(document).ready(function(){
   $('<audio id="chatAudio"><source src="../sound/notify.ogg" type="audio/ogg"><source 
           src="../sound/notify.mp3" type="audio/mpeg"><source src="../sound/notify.wav" 
           type="audio/wav"></audio>').appendTo('body');

   var notificationInterval = setInterval(function(){notification();}, 3000);
});
Rob Schmuecker
  • 8,934
  • 2
  • 18
  • 34
  • mmmm, this good, thnk for your solution.. By the way in the manufacture of JS if any will result in memory ram ride continue? – Whendy Jul 21 '14 at 13:20
  • If I understand correctly you're wondering whether this should increase RAM usage continuously? I don't think so. Have a look though in the Profile tab of Google Chromes developer tools if you are concerned. – Rob Schmuecker Jul 21 '14 at 13:23