代码有点混乱(我认为),但我希望你能理解,(是的,我知道我的 Mysql API 不是最好的,但有时间我会改变(这是一个大脚本))
好吧,我有一个 PHP 脚本,它每 5 秒运行一次,如果在friend_requests 表中存在带有用户的 user_id 的任何行,它会在 Jquery 中发出通知,如果存在则运行 jquery 通知(类似于 facebook )说用户已向他发送了好友请求。
但问题是 PHP 脚本每 5 秒运行一次,但是脚本中的 Jquery 函数说要在屏幕中打开通知框,只运行一次。如果我有一行用户的 user_id,就运行通知是否是第一次运行 php 代码,(如果前 5 秒已经过去,如果该行刚刚在 5 秒之后出现,则不会出现通知框),如果该行出现在前 5 秒. (但 PHP 代码的其余部分运行完美)
朋友请求通知.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"> </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();
});
通知.js
window.setInterval(function(){
$('#notifications').load('friend_request_notification.php');
}, 5000);