2017-07-04 145 views
0

由於某種原因,在location.reload()之前正在處理方法顯示。下面是我的語法:爲什麼刷新html頁面很慢

$(document).ready(function() { 
 
    $("#refresh").click(function() { 
 
    alert("before refresh") 
 
    location.reload(); 
 
    display() 
 
    }) 
 
}) 
 

 
function display() { 
 
    alert("hello world") 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="refresh"> 
 
    refersh and continue 
 
</button>

我在這裏的目標是在點擊按鈕時,然後調用顯示方法刷新頁面。 問題是在刷新頁面之前調用顯示方法。首先,我正在努力實現的目標。如果沒有,還有什麼其他的方法可以解決這個問題?

+0

您需要將刷新方法放入文檔中,並且在單擊'#refresh'元素時應該設置爲任何值的條件內。也許你可以使用'localStorage()'這個 – Ionut

+3

'首先,我試圖達到的目標是 - 是的 - 但不是你嘗試的方式。你需要在頁面的加載中顯示alert()。在調用卸載事件之後,您不能在頁面中執行任何操作(因爲當頁面不再存在時,會調用reload())。 –

+2

@RoryMcCrossan看起來不錯,但它實際上是調用'display()'函數,即使它位於'location.reload()'之後。這是爲什麼? – Barmar

回答

0

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 

 

 
<script> 
 
$(document).ready(function(){ 
 
display(); 
 
    $("#refresh").click(function(){ 
 
    alert("before refresh") 
 
    //location.reload(); 
 
    display(); 
 
    }); 
 
}) 
 

 
function display(){ 
 
    alert("hello world") 
 
} 
 

 
    </script> 
 
<button id="refresh"> 
 
refersh and continue 
 
</button> 
 

 

 
</body> 
 
</html>

嘗試此,調用的顯示方法上加載文檔並取出重載()中,如果根據需要添加顯示器()的刷新。

0

您可以嘗試將您的display方法分配給document.onload事件。嘗試將您的點擊處理程序更改爲:

alert('before refresh'); 
$(document).on('load', display); 
location.reload;