I have a store and I want to play a sound just after receiving an order. I store my orders in my database so I want to play a 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 am logged in. If there are any pending orders I want to play a sound for 30 sec to notify me.
Create an audio
element:
var audio = document.createElement('audio');
Insert it into the body:
document.body.appendChild(audio);
Set the sound you wish to play:
audio.src = 'path/to/filename.ogg';
Whenever a query finishes, play the sound:
audio.play();
Example that plays a sound every five seconds with setInterval
: http://jsbin.com/uravuj/4
So for whatever page you're on, you'd add an ajax function that fires the PHP script that does the query. If it returns true, trigger a javascript function that plays the sound. If it returns false, 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();
});
This assumes that you are returning the data from php as json and that you have already built an audio object as suggested earlier by Delan.
My javascript/jquery is a bit rusty, feel free to comment or edit mistakes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With