I have a page with SQL Queries on inside a table which show results on a large screen.
I then browse to index.php which contains this code:
// <![CDATA[
$(document).ready(function() {
// This part addresses an IE bug. without it, IE will only load the first number and will never refresh
$.ajaxSetup({ cache: false });
setInterval(function() {
$('.container').load('data.php');
}, 2000); // the "2000" here refers to the time to refresh the div. it is in milliseconds.
});
// ]]>
HTML :
<div class="container"><h3>Loading Data...</h3></div>
So it loads this page constantly.
What i would like to do is have it so if any of the queries contain data that needs to have action taken on it, the table cell will flash 2 colours and also a sound will play every 5 minutes.
What would be the best way to do this and keep the constant page load?
I would change the .load() into an ajax call, which calls a function when done. Check the script below:
// Prepare the audio - replace the link with your own mp3
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'http://www.uscis.gov/files/nativedocuments/Track%2090.mp3');
// Global that will control the flashing / playing of sound
var alertUser = false;
$(document).ready(function() {
$.ajaxSetup({ cache: false }); // Fix IE bug
setInterval(function(){
$.ajax({
url: "data.php",
complete: function( jq, content ){
$('.container').html( jq.response );
if( jq.response.indexOf( 'from' ) != -1 ) { // your logic goes here to decide the warning
alertUser = true;
} else {
alertUser = false;
}
}
});
}, 2000);
setInterval(function(){
if( alertUser ) {
// play tune
audioElement.play();
// flash background
window.setTimeout( function(){
$('.container').css('background-color','red')
}, 200 );
window.setTimeout( function(){
$('.container').css('background-color','blue')
}, 400 );
window.setTimeout( function(){
$('.container').css('background-color','transparent')
}, 600 );
}
}, 1000);
});
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