我正在尝试获取 url 的内容并将其附加到我的div,每隔 10 秒,我对 javascript 很陌生,真的不知道为什么这不起作用,任何光棚都会很棒。
我的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);
});
};
我知道生成帖子的页面正在工作,因为我可以在浏览器中看到它们。
你应该执行你的函数:
$(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);
};