使用jquery更新div而不加载像facebook ticker这样的加载页面

时间:2014-12-15 11:10:05

标签: javascript jquery

我是java脚本的新手。

我有facebook风格的右侧栏,显示朋友更新的所有新活动/帖子。在这里,我使用ajax在当前页面上的div内加载单个php。

它运作良好但不能像facebook那样工作。

问题1: 我只想要它(Ticer div)perpand新事件/帖子,如果在10秒后在indivisual php上找到。怎么做。

问题2: 如何在像facebook这样的自动收报机中显示特定事件悬停的完整事件/帖子。我在这里使用了一个悬停脚本,我无法隐藏在身体的任何地方。

我的装载div的ajex:

$.ajaxSetup({ cache: false });
var auto_refresh = setInterval(function() {
    $('#updatetime').load('updateside.php?');
}, 10000); // refresh every 10000 milliseconds

我的悬停脚本

$(".upbox1").mouseover(function() {
    var pos = $(this).position();
    var width = $(this).outerWidth();
    $("#menu").css({
        position: "absolute",
        top: pos.top + "px",
        left: (pos.right + width) + "px"
    }).show();
});

PHP:

$a= mysqli_query // Which find out all followers of me
$b= mysqli_query // which find out all of my activ post/parent id

// Updateside a particular table which add total user all post updates
// So I need a parent id/post which is related with a user activity.

$g = mysqli_query($dbh,"SELECT parent_id FROM updateside WHERE `from_id`='".$followname."' OR `to_id`='".$followerbdid."' OR `from_id`='".$session->username."' OR `to_id`='".$session->bdid."' GROUP BY parent_id DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($g)) {
$parent = $row['parent_id'];

$u = mysqli_query($dbh,"SELECT * FROM updateside WHERE `parent_id`='".$parent."' ORDER BY created DESC") or die(mysqli_error($dbh));
while ($row = mysqli_fetch_array($u)) {
$from_id = $row['from_id'];
$parent_id = $row['parent_id'];
$to_id = $row['to_id'];
$sub = $row['sub'];
$detail = $row['detail'];
$img = $row['img'];
$time = $row['created'];

echo"result will be goes here";

1 个答案:

答案 0 :(得分:1)

您正在使用ajax轮询来获取更新的帖子和事件。所以现在,您只需要使用javascript展示内容的帖子/事件。

首先,您可以检查每个调用的success是否有新帖子,如果有,则使用.prepend()添加帖子。

其次,Ideally you should delegate to the nearest parent that exists whenever you are dealing with dynamically generated content。否则,您的脚本将无法处理动态生成的内容。

$(parent).on("mouseover", ".upbox1",function(){
    ...
});

第三,创建隐藏它的功能,只要点击当前帖子以外的任何地方。

  1. 创建一个半透明/透明的div,其z-index小于 #menu&安培;大于1,宽度为100%,高度为100%,同时将鼠标悬停在图像上

  2. 将一个函数附加到此div上,单击它将隐藏自身 以及#menu

  3. 我假设您已成功通过ajax调用获取数据,而您正在使用#menu来展示您的帖子。

    要仅获取新帖子,您需要执行以下操作: -

    1. 在后端,您需要使update.php接受查询字符串中的timestamp参数(您必须在创建发布/事件时存储时间戳)。返回数据时,如果给定,则使其返回自时间戳以外的所有内容,而不仅仅返回所有数据。否则返回一切。

    2. 第一次加载页面时,首先要做的是对一个返回当前服务器时间戳的新PHP文件进行Ajax调用。将其值作为数字存储在全局Javascript变量中。

    3. 在Ajax轮询期间,将timestamp变量的值增加(自上次获取后看起来像10000毫秒),然后将其提供给update.php,如url:'/ ajax / update。 php?timestamp ='+ latestFetchTimestamp,

    4. 将其添加到您的页面。

    5. 使用多个条件选择数据MySQL: -

      $timestamp = date("m/d/Y h:i:s a", time() - 10);
      $u = mysqli_query($dbh,"SELECT * FROM updateside
               WHERE `parent_id`='".$parent."'
               AND `datetime` > '".$timestamp."'
               ORDER BY created DESC") or die(mysqli_error($dbh));
      

      请点击此链接了解有关MySQL multiple condition select

      的elobarate知识