0

简单地说,我有一个带有由 CSS 制作并由 HTML 上的 SPAN 类实现的动画通知徽章的菜单栏。如果该计数为 0,此徽章应显示新消息的计数并消失。我需要每 1 秒检查一次,但没有运气。

我的js:

<script type="text/javascript">
function removeAnimation(){
setTimeout(function() {
$('.bubble').removeClass('animating')
}, 200);            
}

setInterval(function() {
$.ajax({
    type: 'GET',
    url:  'models/bubble.php',
    data: 'html',
    success: function(response){
    document.getElementById("bub").innerHTML=response;
    document.getElementById("bub").className = "bubble";
    }
    error: function(response) {
    document.getElementById("bub").className = "hidden";
    });
}
}, 1000);
</script>

我的 php (工作和测试):

<?php
require_once("db.php");
$receiver = $this_user
$isnew = 1;
$stmt = $mysqli->stmt_init();
if ($stmt->prepare("SELECT * FROM messenger
    WHERE receiver = ? AND isnew = ? ORDER BY timestamp")) {
    $stmt->bind_param("si", $receiver,$isnew);
    $stmt->execute();
    $stmt->store_result();
    $ttlrows = $stmt->num_rows;
    $stmt->close();
}
echo $ttlrows;
?>

有什么帮助吗?

4

1 回答 1

0

下面对我有用。您需要在成功内再次调用该函数,因为 setTimeout/setInterval 在您提到的延迟后只会调用一次该函数

setTimeout('pullNewMessageCount()', 100);


 function pullNewMessageCount() {
  var url = 'PHP Url';
  $.ajax({
    url: url,
    dataType: 'html',
    data:{
      "whatever data you want to send, skip it if not require",
    },
    type: 'POST',
    success: function(latestCount) {
      setTimeout('pullNewMessageCount()', 100);// Important comment (this what you need to do)
    },
    error: function(jqXHR, textStatus, errorThrown) {
    }
  });
}
于 2013-05-18T08:15:23.960 回答