我正在使用AJAX调用点击一个按钮并在显示相同页面的所有主机中显示一个div ,其id被称为,然后div在n秒后隐藏,并且如果再次单击该按钮,则会出现。
问题是div只出现然后隐藏在用户的主机中,点击按钮而不是其他用户(如果我删除if语句,它将出现在所有主机上,但它不会隐藏,然后在新点击按钮时再次显示)
以下是JQUERY代码:
$(document).ready(function(e){
$.ajaxSetup({cache:false}); // IE BUG -> caso contrário IE não faz refresh
setInterval(function() {$('#chatlogs').load('logs.php');}, 500);
// setInterval(function() {$('#not').show().load('logs1.php').delay(5000).fadeOut();}, 500);
var n=0;
$('#submeter').click(function(){
if(n==0){
$('#not').show();
setInterval(function() {$('#not').load('logs1.php');}, 500);
n=1;
}
if(n==1){
$('#not').delay(2000).fadeOut();
n=0;
}
})
});
更新logs.php:
<?php
$con = new mysqli("localhost","root","root","efansredes");
$results=$con->query("SELECT * FROM chat ORDER by id DESC");
$i = 0;
while($row=$results->fetch_array()){
if($i %2 == 0)
{
$class = 'even';
}
else
{
$class = 'odd';
}
$i++;
// output da tabela
echo '<div class="alert alert-success" role="alert">';
echo "<table>";
echo "<tr class='$class'>";
echo "<td>"."<b>".$row["username"]."</b>".": ".$row["msg"]."</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
?>
更新logs1.php
<?php
$con = new mysqli("localhost","root","root","efansredes");
$results=$con->query("SELECT * FROM chat ORDER by id DESC LIMIT 1");
$i = 0;
while($row=$results->fetch_array()){
if($i %2 == 0)
{
$class = 'even';
}
else
{
$class = 'odd';
}
$i++;
// output da tabela
echo '<div class="alert alert-success" role="alert">';
echo "<table>";
echo "<tr class='$class'>";
echo "<td>"."<b>".$row["username"]."</b>".": ".$row["msg"]."</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
?>