小编典典
这就是我最近使用jQuery实现解决方案的方式。
每当发生 重大 更新时,PHP都会在数据库中增加一个字段。
// Call this function when data changes
function update_clients()
{
mysql_query( "UPDATE pageGen SET id = id + 1 LIMIT 1" );
}
// Call this function to get the ID to pass to JavaScript
function get_update()
{
$result = mysql_query( "SELECT id FROM pageGen LIMIT 1" );
$update = mysql_result( $result, 0, 'id' );
return $update;
}
?>
最初加载页面时,使用数据库中的数字填充JavaScript变量:
var pageGenID = 25218603 // generated by PHP
var processUpdate = function( response )
{
if ( pageGenID < response )
{
replace_current_data_with_new_via_ajax();
pageGenID = response;
}
}
// Compare our Page Generate ID against that of the server
var checkUpdates = function()
{
serverPoll = setInterval( function()
{
$.get('script_to_return_latest_pageGenID.php',
{ lastupdate: 1 },
processUpdate, 'html');
}, 10000 )
};
// Check for updates every 10 seconds
$( document ).ready( checkUpdates );
2020-05-29