这就是我最近使用jQuery实现解决方案的方式。
每当发生 重大 更新时,PHP都会在数据库中增加一个字段。
<?php// Call this function when data changesfunction update_clients(){ mysql_query( "UPDATE pageGen SET id = id + 1 LIMIT 1" );}// Call this function to get the ID to pass to Javascriptfunction get_update(){ $result = mysql_query( "SELECT id FROM pageGen LIMIT 1" ); $update = mysql_result( $result, 0, 'id' ); return $update;}?>最初加载页面时,使用数据库中的数字填充Javascript变量:
<script type="text/javascript">var pageGenID = 25218603 // generated by PHPvar processUpdate = function( response ) { if ( pageGenID < response ) { replace_current_data_with_new_via_ajax(); pageGenID = response; }}// Compare our Page Generate ID against that of the servervar 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 );</script>


