Here's the situation.
I have a database with the following columns: id, last_updated, class, name, score, active, bonus - all of which serve a purpose of other things other than what I need assistance with. the page I have, calls a jQuery function that launches an ajax call to the database sorts the table by last_updated, picks the first one and displays the results on the page every so often.
The thing I'm wondering about is if there is a way to call the function on document ready, but not execute it until the first update to the database?
DATABASE:
| id | last_updated | class | name | score | active | bonus |
| 1 | 2017-05-02 11:45:06 | classA | name01 | 0 | 1 | 0 |
| 2 | 2017-05-02 11:44:18 | classB | name02 | 0 | 1 | 0 |
| 3 | 2017-05-02 11:43:58 | classC | name03 | 0 | 1 | 0 |
| 4 | 2017-05-02 11:43:47 | classD | name04 | 0 | 1 | 0 |
JS:
$(document).ready( function() {
pingScores();
});
var timestamp = null;
var lastNotificationIdentifier = timestamp;
function pingScores() {
$.ajax({
type: "GET",
url: "getUpdate.php",
cache: false,
async: true,
success: function(data){
var json = eval('(' + data + ')');
var notinyClass = json[2];
var notinyName = json[3];
var localIdentifier = json[1] + json[0];
if (localIdentifier !== lastNotificationIdentifier) {
if (json[6] == 0){
$.notiny({ text: notinyName+"<span class='addScore'> +10</span>", width: '100%', image: notinyClass });
lastNotificationIdentifier = localIdentifier;
}
}
setTimeout("pingScores()", 10);
}
});
};
PHP:
<?php
require "config.php";
$sql = "SELECT * FROM roster ORDER BY last_updated DESC LIMIT 1";
$result = $conn->query($sql);
$array = mysqli_fetch_row( $result );
echo json_encode($array);
$conn->close();
?>
I know that what I have does exactly what I don't want it to do, but I don't really know how to execute it the way I want to, where I initialize the ajax call, then execute on database update.
Thanks in advance.