i want make a real time notification with jquery and play sound first play when a new entry added , but my script the audio play repeated
front end :
<script>
setInterval(
function()
{
$('#orderjum').load('refresh/jumlahorder.php');}, 1000);
</script>
backend :
<?
require("../../include/koneksi.php");
$sql = "Select * FROM pemesanan";
$result = mysqli_query($konek,$sql) or die(mysqli_error($konek));
$num_rows = mysqli_num_rows($result);
echo "<h3>$num_rows</h3>
<p>Total Orders</p> <audio controls autoplay hidden='true'>
<source src='http://www.w3schools.com/html/horse.ogg' type='audio/ogg'>
</audio>";exit;
?>
U should create a script (in PHP ) so that u can have a option to check with JS AJAX if anything changed. The PHP file (say name of the file is - backend_file.php ) will just return true if anything updated and false if no new event happened.
So, in that case the JS code for that would be like this-
setTimeout("checkUpdate()",1000); //poll every second function checkUpdate() { $.post("backend_file.php", function(data, status) { if (data.toString()=="true") { playSound(); } }); } function playSound() { var audio = new Audio('http://www.rangde.org/static/bell-ring-01.mp3'); audio.play(); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.