PHP / JQuery - PHP脚本每5秒运行一次,但其中的JQuery函数只运行一次
PHP / JQuery - PHP Script runs every 5 seconds, but jquery function in it runs just once
这是有点混乱的代码(我认为),但我希望你理解它,(是的,我知道我的Mysql API不是最好的,但我会改变当我有时间(这是一个大脚本))
好吧,我有一个PHP脚本,每5秒运行一次,这在Jquery通知如果存在任何行在friend_requests表与用户的user_id,如果存在运行一个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"> </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);
这将强制浏览器在每次发送请求时下载一个新文件。
我还没有测试过,但我猜它只加载一次,因为它第一次加载它并缓存文件。因此,它本身不会运行第二次。
这是有意义的,否则该人的缓存将有100个相同文件的副本,这是没有用的。
当interval运行时,它会运行javascript文件中的函数。
window.setInterval(function(){
$('#notifications').load('friend_request_notification.php');
showTip()
}, 5000);
或类似。你不应该在这样的脚本中一遍又一遍地加载一个javascript文件。这个JS应该在首页
重要的是要意识到document.ready
已经发生在您正在加载新内容的主页上。
这意味着当你加载新脚本时,它会立即触发,在这种情况下,它会在它引用的html存在之前触发。
将脚本放在friend_request_notification.php
中的html之后,将确保html在脚本触发之前被加载到DOM中。
结构上,你可以考虑只是包装所有的代码在jquery.facebookBeeper.js
到一个函数包含在notifications.js
,而不是每5秒对该脚本文件的新请求,在load
的完整回调中运行你的函数。
window.setInterval(function(){
$('#notifications').load('friend_request_notification.php',function(){
/* new content now exists in page, run code here*/
});
showTip()
}, 5000);
还应该注意,当您在页面加载时声明var a = $("#BeeperBox"),
,然后用新html替换该元素时,var a
将不会是具有相同ID的新版本元素的引用。
- JavaScript 只在 php 代码中执行一次
- Ajax发布到PHP脚本,每5秒查询一次MySQL数据库的性能/问题
- 在php中使用jquery每隔5秒更改一次图片
- 如何附加一个id's的详细信息只通过php或jquery提供一次
- Ajax 使用函数每秒获取一次 php 数据
- 为什么PHP一次发送所有内容
- 每次点击一次,jQueryAJAX就会发布到PHP
- XMLhttpRequest Ajax PHP 运行不止一次
- 尝试连续一次显示多个 php 文件
- 背景图像旋转器.php + jQuery每4秒显示一次随机照片
- PHP 在输入内单击一次
- 使用 jQuery 每 xy 秒执行一次 PHP 脚本
- 嵌套在jQuery中的PHP只执行一次.我希望它每次都执行
- 一个静态 php 变量,只分配一次,永远不会更改
- AJAX 请求只发生一次(PHP 表单请求)
- 每小时自动运行一次PHP页面 - 无需使用Cron作业
- PHP 正在鼠标移动到窗口上执行 JavaScript.它循环在一个函数上.我怎样才能让它只执行一次
- PHP 重新加载网站一次
- 在 PHP 中包含/要求一次
- 为什么Javascript函数在php循环中只执行一次