2011-08-25 50 views
0

我有一個跨度標籤的變量:

echo "<span class='headings_sub' id='msgcntDiv'>You have ".$numOfMessages." </span>"; 

我的腳本:

<script> 
    $(document).ready(function() { 
     var reload = function(){ 
     $("msgcntDiv").load("newMessageCnt.php"); 
    } 
    window.setInterval(function() { 
     $("#msgcntDiv").load(reload); 
    }, 10000); 
    }); 
</script> 

「newMessageCnt.php」 有句臺詞:

<?php 
include('header_application.php'); 
$pageValue = "dashboard"; 
$obj_clean->check_user(); 
echo $numOfMessages = $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']);  
?> 
+0

我瘦必須用'負載(」。 ...');'而不是'.data(...)' – yvan

回答

0

看來你正在尋找爲$ .load,而不是$ .data。此外,您可能需要調用它的間隔或重複的超時:

<script> 
$(document).ready(function() { 
    var reload = function(){ 
     $("msgcntDiv").load("newMessageCnt.php"); 
    } 
    window.setInterval(reload, 10000); 

}); 
</script> 
+0

你的意思是$(「msgcntDiv」)。load();) –

+0

是的,複製粘貼,並忘記編輯,因爲我的手機響了。謝謝:) –

+0

在這種情況下,不會使用'setTimeout'遞歸安全嗎?假設服務器出現問題,它會阻止繼續提出請求。另請參閱:http://stackoverflow.com/questions/729921/settimeout-or-setinterval/731625#731625 – Geert

0
<script> 
    $(document).ready(function() { 
     refresh(); 
    }); 

    function refresh() 
    { 
     $.get('getUnopenedMessage.php', function (cnt) { 
      // $("#msgcntDiv").data('cnt', cnt); 
      $("#msgcntDiv span").text(cnt); 
      setTimeout(refresh(), 10000);    
     }); 
    } 
</script> 

和文件getUnopenedMessage.php,或多或少:

<?php 
//session_start(); 
$obj_clean = new yourMailClass('...'); 
echo $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']) 
?> 
+0

可能是setTimeout(refresh,10000); ? –

+0

它確實調用我的.php文件,但是它不會更新我的變量

+0

會執行'$(「#msgcntDiv」).text(cnt);'訣竅? $ .data()是一個jquery函數,用於設置並獲取「私有屬性」,讓你選擇的元素。我想你有另一個jquery函數讀取這個cnt數據,以顯示新郵件的數量。 @Darhazer:是的!感謝名單!編輯! ^^ – roselan