jquery/ajax load new content when available

倾然丶 夕夏残阳落幕 提交于 2019-12-01 01:42:41

Obviously the first thing that springs to mind is a lightweight AJAX request every x seconds to check for new content.

Something along the lines of http://buntin.org/2008/09/23/jquery-polling-plugin/

Edit: This is untested.

AJAX call to http://example.com?lastCheck=1273244156

PHP: 
<?
    if(isset($_GET['lastCheck'])){
        $ts = mysql_real_escape_string($_GET['lastCheck']);

        $result = mysql_query("SELECT * FROM `table` WHERE `timestamp` >= {$ts}");
        $rows = mysql_fetch_array($result, MYSQL_ASSOC);

        if($rows){
            header('Cache-Control: no-cache, must-revalidate');
            header('Content-type: application/json');
            echo json_encode($rows);
        }
    }
?>

Then use jQuery to check if the AJAX response is valid JSON, if so build your items and append them to your container.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!