Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play a sound after a query?

Tags:

javascript

php

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.

like image 233
hamp13 Avatar asked Feb 24 '23 07:02

hamp13


2 Answers

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

like image 106
Delan Azabani Avatar answered Mar 05 '23 15:03

Delan Azabani


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.

like image 25
Anthony Avatar answered Mar 05 '23 14:03

Anthony