这是我所做的并使用了您标记的答案,但我认为那里有一些地方需要改变。
在主页中(如果数据库发生更改,您要刷新该页面)。
我在我的数据库中创建了一个名为 setting 的表,在这个表中我创建了一个名为 rowCounter 的行。
当您检查的表中的行数已更改时,正在更新 rowCounter。
所以我的代码分成两个块。一个给我设置中的数字> rowCounter
第二个是给我表格中当前数字的脚本。如果它们不相等,则刷新页面。
所以这是您需要的第一个文件
script_to_return_latest_pageGenID.php
// here you will make you connection query.
$result = mysql_query( "SELECT rowCounter FROM setting WHERE `id`='2'" );
$update = mysql_fetch_assoc($result);
echo implode($update);
$count=mysql_query("SELECT `id` FROM log_".$datestamp."")or die(mysql_error);
$number = mysql_num_rows($count);
//echo $number;
$countFromSetting=mysql_query("SELECT `rowCounter` FROM setting WHERE id='2'")or die(mysql_error);
$numberFromSetting=implode(mysql_fetch_assoc($countFromSetting));
if($number != $numberFromSetting)
{
mysql_query("UPDATE setting SET `rowCounter`='$number' WHERE `id`='2'")or die(mysql_error);
}
在主页中,您将编写我提供的两个代码块,您应该将其放在脚本之前。
$countFromSetting=mysql_query("SELECT `rowCounter` FROM setting WHERE id='2'")or die(mysql_error);
$numberFromSetting=implode(mysql_fetch_assoc($countFromSetting));
在此代码之后,您将每隔几秒查询一次 php 脚本的脚本,检查数字是否不相等,它将刷新页面。
var pageGenID = "<?php echo $numberFromSetting; ?>";
var processUpdate = function( response ) {
var x=response;
//console.log(pageGenID); by removing the remarks you will see the compared numbers all the time.
//console.log(x);
if ( pageGenID != x )
{
//replace_current_data_with_new_via_ajax();
pageGenID = response;
window.location.reload();
}
}
var checkUpdates = function()
{
serverPoll = setInterval( function()
{
$.get('script_to_return_latest_pageGenID.php',
{ lastupdate: 1 },
processUpdate, 'html');
}, 5000 ) };
$( document ).ready( checkUpdates );