且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的/ AJAX加载新的内容时可用

更新时间:2023-12-05 08:17:40

显然,这弹簧想到的第一件事情是一个轻量级的AJAX请求每x秒,以检查是否有新的内容。

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

沿 http://buntin.org/2008/的东西线9月23日/ jQuery的轮询-插件/

编辑: 这是未经测试。

AJAX调用 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);
        }
    }
?>

然后使用jQuery来检查,如果Ajax响应是有效的JSON,如果是建立你的项目,并把它们添加到您的容器。

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