I have a php file that returns new items from db and using ajax, i update a div with number of new items.
In order to make it look a bit like facebook, i have inside a setInterval(3000 ms)
the ajax call.All ok here.
Now i want to play a sound when new items comes up.
My code looks like this:
$.ajax({
type: "POST",
url: "script.php",
data: { action : somedata },
cache: false,
success: function(html){
if(html > 0) {
// update div
}else{
//do nothing
}
}
})`
I googled how to play a sound in jQuery, and i got a working solution:
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'notification.ogg');
audioElement.setAttribute('autoplay', 'autoplay');
$.get();
audioElement.addEventListener("load", function() {
audioElement.play();
}, true);
If i put the previous code here:
if(html > 0) {
here
}
the sound is played, but there's a problem. It's played every 3000ms. I don't want this. I want to play it only when a new item comes up in the db, and not every time the ajax call is made.
How can i achieve this?