0

我的页面上有这段代码...... jQuery

window.setInterval( function(){
        $.get("php/get_posts.php", function(data) {
                 $('.post-container').prepend(data);
            });},10);

这是get_posts.php

<?php
include('dbconnect.php');
session_start();
$uid= $_SESSION['uid'];
$get_ids=mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($get_ids)){
$id=$row['id'];
$sm=$row['message'];
}
$get_lpid=mysql_query("SELECT * FROM users WHERE uid='$uid'");
while($row_o = mysql_fetch_array($get_lpid)){
$l_pid=$row_o['lastviewed'];
}
if($id!=$l_pid){
    $insert=mysql_query("UPDATE users SET lastviewed='$id' WHERE uid='$uid' "); 
    if($insert){?>
           <div class='media'><img src='img/profile_pictures/thumbs/thumb_13718921232_119055628287843_1500172795_n.jpg' class='img-circle  post-circle pull-left'><div class='media-heading'><a href='#'>Pratik Sonar</a><div class='pull-right'><small>12.00PM</small></div></strong></div><div class='media-body'><?php echo $sm ?></div></div>
    <?php } else{
    }
  }
else{
}?>

这种技术似乎适用于除 chrome 之外的所有浏览器。我已经测试过,即 safari、firefox 和 opera 都可以正常工作。任何人都可以在这件事上启发我吗?有什么我不知道或我想念的吗?

4

3 回答 3

0

尝试将您的代码包装到此函数中:

$(document).ready(function() { ... });

喜欢:

$(document).ready(function() {
window.setInterval( function(){
        $.get("php/get_posts.php", function(data) {
                 $('.post-container').prepend(data);
            });},10);
});

你可能也最好使用setTimeout()

现在代码在 DOM 完全加载时运行。

于 2013-07-02T11:36:15.290 回答
0

为什么要使用 window.setInterval?它只是 setInterval(),没有任何父级。

尝试

$(document).ready(function() {
    setInterval(function(){
        $.get("php/get_posts.php", function(data) {
             $('.post-container').prepend(data);
        });
    },10);
});
于 2013-07-02T16:39:21.707 回答
0

谢谢大家的关心。好吧,最后这个错误被这段代码修复了。我猜 setTimeout 获得了对 setInterval 的胜利

$(document).ready(function() {
    window.setTimeout(function(){
        $.ajax({
        type: "GET",
        url: "php/get_posts.php",
        }).done(function( data ) {
            $('.post-container').prepend(data);
        });
        },10);
});
于 2013-07-03T09:10:31.203 回答