weixin_33728268 2013-09-09 00:54 采纳率: 0%
浏览 23

Ajax提要自动刷新

I got an auto-refresh snippet of code called News feed master and I have managed to implement and customize it to my own taste. Everything seems to be working except the auto-refresh which is meant to show my new post as I input it instead of having to refresh the whole page.

Here is a section of my index.php that queries the database for new feeds

<?php
/**
* Preparing and getting response for latest feed items.
**/
if(isset($_POST['latest_news_time'])){
$result = $db->query("SELECT *
FROM users U, messages M WHERE U.user_id = M.uid_fk AND M.created > '".$_POST['latest_news_time']."' ORDER BY M.created DESC");
$current_time = $_POST['latest_news_time'];
$item = $db->fetch_assoc($result);
$last_news_time = $item['created'];
while ($last_news_time < $current_time) {
    usleep(1000); //giving some rest to CPU
    $item = $db->fetch_assoc($result);
    $last_news_time = $item['created'];
}
?>
            <li id="<?=$item['created']?>">
            <div class="liSub">
                <div class="feed_image"><img src="uploads/user/<?=$item['image']?>" alt="<?=$item['image']?>"></div><!-- image -->
                <div class="head"><a href="profile.php?user=<?=$item['username']?>"><?=$item['first_name']?> <?=$item['last_name']?></a><?php if($item['status']=='politician')print"<img src='images/tick.png' alt='verified' class='verified'>";?></div><!-- username -->
                <div class="feedtext"><span><?=$item['message'];?></span></div><!-- message -->
                <div class="bottom">
                <p><?=$item['like_count']?></p><a href="#"><img src="images/Like-button.png" alt="like" class="like"></a><!-- like -->
                <p><?=$item['dislike_count']?></p><a href="#"><img src="images/disLike-button.png" alt="dislike" class="dislike"></a><!-- dislike -->
                <p><?=$item['comment_count']?> Comment(s)</p><!-- comments -->
                </div>
            </div>
            </li>
<?php
exit;
}
?>

And here's a section of my javascript that is supposed to perform the auto-refresh

/**
* Function to update the news feed
**/
function updateFeed(){
    var id = 0;
    id = $('#feeds li :first').attr('id');
    $.ajax({
        'url' : 'index.php#feeds ul',
        'type' : 'POST',
        'data' : {
            'latest_news_time' : id  
        },
        success : function(data){
            setTimeout('updateFeed()', 1000);
            if(id != 0){
                $(data).prependTo("#feeds ul");
            }
        }
    }) 
}

I would be glad if you could help spot out the problem. I'm not a pro in jquery at all so it's giving me sleepless nights. Thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2013-09-09 02:16
    关注

    there is an optional cache which the default value is true. add this somewhere in your ajax call:

    'cache': false,
    

    Also, you may be getting a hidden error because you have no error handler:

    error: function(jqXHR, textStatus, errorThrown){
    
        alert(textStatus);
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型