2013-05-18 43 views
0

簡單地說,我有一個菜單欄,上面有由CSS製作的動畫通知標誌,並由SPAN類在HTML上實現。這個徽章應該顯示新消息的數量並且如果該計數爲0則消失。我需要每隔1秒檢查一次,但沒有運氣。Ajax定時更新問題

我的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; 
?> 

任何幫助嗎?

+0

的延遲之後會調用該函數只有一次什麼問題 –

+0

它不讀取PHP的輸出,在舊版本的我的代碼它只讀取一次輸出。 –

回答

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) { 
    } 
    }); 
} 
+0

好的,但你錯過了用id(bub)更新我的跨度內容的部分以顯示正確數量的徽章,並且我想通過分配類(隱藏)來隱藏同一跨度(如果計數爲0)它有點像facebook通知系統,氣泡id的跨度是(bub),氣泡動畫類是(.animating),隱藏類是(.hidden) –