2017-07-04 149 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;