How can I play sound after request?

I have a store and I want to play the sound immediately after receiving the order. I save my orders in my database, so I want to play the sound after running this check order query:

$order_check_query("SELECT * FROM orders WHERE status = 'pending'"); 

I want to run this query every 5 minutes when I logged in. If there are any pending orders, I want to play the sound for 30 seconds to notify me.

+4
source share
2 answers

So, for any page you are on, you must add an ajax function that runs a PHP script that executes the request. If it returns true, activate the javascript function that plays the sound. If it returns false, there is no sound. Here is an example with jquery:

 function checkOrders() { $.get('checkOrders.php', function(data) { if(data.neworders == true) { audio.play(); } } }); t=setTimeout("checkOrders()",(5 * 60 * 1000)); } $(function() { checkOrders(); }); 

It is assumed that you are returning data from php as json and that you have already created an audio object as suggested by Delan earlier.

My javascript / jquery is a little rusty, feel free to comment or edit errors.

+2
source

Create an audio element:

 var audio = document.createElement('audio'); 

Insert it into the body:

 document.body.appendChild(audio); 

Set the sound you want to play:

 audio.src = 'path/to/filename.ogg'; 

Whenever the request ends, play the sound:

 audio.play(); 

An example that plays sound every five seconds using setInterval : http://jsbin.com/uravuj/4

+6
source

All Articles