2012-08-16 41 views
0

我正在嘗試獲取url的內容並將其附加到我的div上,每隔10秒,我對JavaScript很陌生,真的不知道爲什麼這是沒有工作,任何光棚都會很棒。試圖用jquery ajax和php更新新帖子

我的javascript:

$(document).ready(function() { 
function get_new_posts(){ 
    var new_posts = $.ajax({ 
         type: "POST", 
         url: "/ajax/get_latest_posts/", 
         dataType: "html", 
         cache:false, 
         async: false 
        }).success(function(){ 
         setTimeout(function(){get_new_posts();}, 10000); 
        }).responseText; 

    $("#posts_container").append(new_posts);   
}); 
}; 

我知道生成帖子頁面正在工作,我可以在瀏覽器中看到它們。

回答

2

你應該執行你的函數:

$(document).ready(function() { 
     get_new_posts(); 
}); 

function get_new_posts(){ 
var new_posts = $.ajax({ 
        type: "POST", 
        url: "/ajax/get_latest_posts/", 
        dataType: "html", 
        cache:false, 
        async: false 
       }).success(function(){ 
        setTimeout(function(){get_new_posts();}, 10000); 
       }).responseText; 

$("#posts_container").append(new_posts);   

}; 
+0

很好,這是我傻的,是不是。哈! – Zen 2012-08-16 01:10:05