1

我的数据库中有一个表。我使用一个while循环来遍历它们。并使用获取的数据制作一个 HTML div。我使用了 LIMIT 10,因为帖子中的条目会不断变化,因为用户会不断将帖子插入表中

$get_ids=mysql_query("SELECT * FROM posts ORDER BY id LIMIT 10");
while($row = mysql_fetch_array($get_ids)){
            $sm=$row['message'];
            echo "<div>".$sm"</div>";
}

我想知道的是如何使用 jquery 让这个脚本每隔 1 秒左右将这 10 个 div 插入到我的 DOM 中。帮助,请紧急!!!!

4

3 回答 3

1

您将该代码放在一个单独的文件中(例如:divfill.php),然后使用类似这样的东西

$.get({
    'url': 'divfill.php',
    'success': function(data) {
       $("#content").html($("#content").html() + data);
    }
});
于 2013-06-29T18:02:55.813 回答
1

尝试在谷歌上搜索一些 jquery 和 php 网络服务示例。基本上你应该做这样的事情:

//Javascript function which fetches the single data as J.D.Smith suggested
function getHtml()
{
   $.get({
      'url': 'Address of your php webservice',
      'success': function(data) {
           $("#content").html($("#content").html() + data);   //Append html to your page
       }
   });
}

//Call the function every 10 sec, place it in $(document).ready() or anything else you use
window.setTimeout(function () {
        getHtml();
    }, 10000);

此代码更像是说明性示例而不是工作代码

于 2013-06-29T20:23:41.083 回答
0

就我而言,我在我的 js 文件中添加了以下方法,

    //call the target function after 250 ms
function async(targetFn, callback) {
    setTimeout(function () {
        targetFn();
        callback();
    }, 250);
}

而且,我调用这个函数,如下所示,

async(BindValuesInLookup, function () {
         BuildGrid()
         GetInfo();
    });

BindValuesInLookup 是目标函数。它在 250 毫秒后被触发。完成后,将执行回调函数。因此,您可以在循环中使用此功能并将超时时间增加到 1000(1 秒)。

谢谢, 维姆

于 2013-06-29T18:03:19.403 回答