Home » Php » php – JQuery wallboard with sound notifications and flashing div/table cells

php – JQuery wallboard with sound notifications and flashing div/table cells

Posted by: admin July 12, 2020 Leave a comment

Questions:

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?

How to&Answers:

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);
});