2013-10-07 35 views
0

我有这个JS,是为了显示每个动态加载的职位上点击时的代码之间:延时动态的帖子

function showPost(id) { 
    $.getJSON('http://hopeofgloryinternational.com/?json=get_post&post_id=' + id + '&callback=?', function(data) { 
     var output=''; 
     output += '<h3>' + data.post.title + '</h3>'; 
     output += data.post.content; 
     $('#mypost').html(output); 
    }); //get JSON Data for Stories 
} //showPost 

当我测试我的手机或Windows浏览器的页面“http://howtodeployit.com/devotion/”,点击每日灵修信息和我在每篇文章之间导航,我注意到先前访问过的帖子在显示新帖子之前仍然显示几秒钟。

如何刷新页面或DOM,以清除以前访问过的页面。

回答

2

函数以行$('#mypost').html("");开始,然后再转到另一个请求以清除显示内容。

您还可以在显示下一个请求的内容之前添加等待消息$('#mypost').html("Please wait...");

function showPost(id) { 

    $('#mypost').html(""); //add this line 

    //$('#mypost').html("Please wait..."); //also you can add it to show waiting message. 

    $.getJSON('http://hopeofgloryinternational.com/?json=get_post&post_id=' + id + '&callback=?', function(data) { 
     var output=''; 
     output += '<h3>' + data.post.title + '</h3>'; 
     output += data.post.content; 
     $('#mypost').html(output); 
    }); //get JSON Data for Stories 

} 
+0

像消息比特这使得它看起来更加专业。谢谢 – Chelseawillrecover

4

只需empty() myPost的内容,而点击该项目或单击后退按钮。原因是您之前的内容仍然存在于mypost div中,即使在执行ajax调用之前,您的内容页面也可见,这可能需要一些时间才能完成,例如700ms,因此您会在很长一段时间内看到旧内容。

function showPost(id) { 

var $myPost = $('#mypost').empty(); //emtpy it 

$.getJSON('http://hopeofgloryinternational.com/?json=get_post&post_id=' + id + '&callback=?', function(data) { 
    var output=''; 
    output += '<h3>' + data.post.title + '</h3>'; 
    output += data.post.content; 
    $myPost.html(output); 
}); //get JSON Data for Stories 
+0

这解决了问题 – Chelseawillrecover

0

可以清空()$ mypost

var $myPost = $('#mypost').empty();