2013-10-07 34 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();