PHP/JQuery - PHP 脚本每 5 秒运行一次,但其中的 jquery 函数仅运行一次

标签 php javascript jquery mysql ajax

代码有点困惑(我认为),但我希望你能理解它,(是的,我知道我的 Mysql API 不是最好的,但当我有时间时我会更改(这是一个)大脚本))

嗯,我有一个每5秒运行一次的PHP脚本,如果friend_requests表中存在带有用户user_id的任何行,它会在Jquery中发出通知,如果存在则运行jquery通知(类似于facebook)说用户已向他发送了好友请求。

但问题是 PHP 脚本每 5 秒运行一次,但脚本中表示打开屏幕通知框的 Jquery 函数只运行一次。如果我有一行包含用户的 user_id ,如果是第一次运行 php 代码,则仅运行通知(如果前 5 秒已经过去,如果该行在这 5 秒之后出现,则不会出现通知框),只要该行出现在第一个 5 秒(但其余 PHP 代码运行完美)

friend_request_notification.php

<?php include_once("includes/head.php"); ?>
<?php require_once("includes/connect/connect.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/jquery.php"); ?>
<?php login_validation();

function friend_request_notification()
{
    global $db;
    global $userid;  

    $query_id_see = "SELECT user_id FROM friend_requests WHERE user_id={$userid}";
    $result_set3 = mysql_query($query_id_see, $db) or die(mysql_error());

    if ($id_requests = mysql_fetch_array($result_set3))
    {
        $select_requester_id = "SELECT user_id_requester FROM friend_requests WHERE user_id={$userid}";
        $result1=mysql_query($select_requester_id);

        $row = mysql_fetch_assoc($result1);
        $requester_id = $row['user_id_requester'];

        $select_requester_name = "SELECT * FROM members WHERE id={$requester_id}";
        $result2=mysql_query($select_requester_name);

        $row = mysql_fetch_assoc($result2);
        $requester_fname = $row['first_name'];
        $requester_lname = $row['last_name'];

        echo '
        <html>
        <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

       <link rel="stylesheet" type="text/css" href="style2.css" />
       <script src="jquery.facebookBeeper.js" type="text/javascript"></script>
       </head>
       <body>

       <div id="BeeperBox" class="UIBeeper">
         <div class="UIBeeper_Full">
            <div class="Beeps">
               <div class="UIBeep UIBeep_Top UIBeep_Bottom UIBeep_Selected" style="opacity: 1; ">
                  <a class="UIBeep_NonIntentional" href="#">
                     <div class="UIBeep_Icon">
                        <i class="beeper_icon image2"></i>
                     </div>
                     <span class="beeper_x">&nbsp;</span>
                     <div class="UIBeep_Title">
                        <span class="blueName"> ' . $requester_fname . ' ' . $requester_lname . '</span> has send you a friend request <span class="blueName">coise</span>.
                     </div>
                  </a>
               </div>
            </div>
         </div>
       </div>

       </body>
       </html>';

       $insert_table = "INSERT INTO friend_requests_notificated SELECT * FROM friend_requests WHERE user_id={$userid} ";
$delete_table = "DELETE FROM friend_requests WHERE user_id={$userid}";
$change_table1 = mysql_query($insert_table) or die(mysql_error());
$change_table2 = mysql_query($delete_table) or die(mysql_error());

    }
    else
    {

    }   
}

friend_request_notification();
?>
<小时/>

jquery.facebookBeeper.js

$(document).ready(function () {
// set the time for the beeper to be displayed as 5000 milli seconds (5 seconds)
var timerId, delay = 5000;
var a = $("#BeeperBox"),
    b = $("a.control");;
//function to destroy the timeout


function stopHide() {
    clearTimeout(timerId);
}
//function to display the beeper and hide it after 5 seconds


function showTip() {
    a.show();
    timerId = setTimeout(function () {
        a.hide();
    }, delay);

}
    showTip();
//function to hide the beeper after five seconds


function startHide() {
    timerId = setTimeout(function () {
        a.hide();
    }, delay);
}
//display the beeper on cliking the "show beeper" button
b.click(showTip);
//Clear timeout to hide beeper on mouseover
//start timeout to hide beeper on mouseout
a.mouseenter(stopHide).mouseleave(startHide);

$('.beeper_x').click(function () {
    //hide the beeper when the close button on the beeper is clicked
    $("#BeeperBox").hide();
});

showTip();
});
<小时/>

notifications.js

window.setInterval(function(){
    $('#notifications').load('friend_request_notification.php');


}, 5000);

最佳答案

看起来不错,但我猜有一个缓存问题,就像 JavaScript 从缓存中获取相同的文件一样。添加时间戳,以便每次请求一个新文件。

window.setInterval(function(){
    $('#notifications').load('friend_request_notification.php?' + (new Date()).getMilliseconds());
}, 5000);

这将强制浏览器在每次发送请求时下载新文件。

关于PHP/JQuery - PHP 脚本每 5 秒运行一次,但其中的 jquery 函数仅运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15999453/

相关文章:

javascript - ExtJS 以 ISO 8601 格式显示来自服务器的日期/时间

javascript - 使用接受字符串的事件处理程序动态创建元素

javascript - 获取表的 thead 标签内输入的 IDS

java - 将JAVA程序转换为PHP代码

php - 使用 $http.get 从 sql 数据库检索数据

php - json_decode 到数组

php - 通过引用传递对象属性

javascript - 如何在javascript中连接php数据库文件?

javascript - 套接字 IO 重新连接?

javascript - 如果 Obj 中存在数组,则获取数组项,如果不存在则获取字段