使用 JQuery 自动刷新内容而不重新加载页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17323431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Auto refreshing the content without reloading page, using JQuery?
提问by user2493093
I have a jsp page which shows the contents of a table. While the user views a page the contents of the table changes on a second-by-second basis. So the user has to refresh the page every time to see fresh and updated contents. How can i update contents of the jsp page without having to refresh the page.
我有一个显示表格内容的 jsp 页面。当用户查看页面时,表格的内容会逐秒变化。所以用户每次都必须刷新页面才能看到新鲜和更新的内容。如何在不刷新页面的情况下更新jsp页面的内容。
here is my code pls
这是我的代码请
it will not work .Please give me your suggestions and if not give a sample code thanks
它不起作用。请给我您的建议,如果没有,请提供示例代码,谢谢
index.jsp
索引.jsp
<html>
<head>
<Title>Just A Test</Title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_me').load('samp.jsp').fadeIn("slow");
}, 10000); // autorefresh the content of the div after
//every 10000 milliseconds(10sec)
</script>
</head>
<body>
<div id="load_me"> <%@ include file="samp.jsp" %></div>
</body>
/html>
回答by Andy Brudtkuhl
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
var reloadData = 0; // store timer
// load data on page load, which sets timeout to reload again
loadData();
});
function loadData() {
$('#load_me').load('samp.jsp', function() {
if (reloadData != 0)
window.clearTimeout(reloadData);
reloadData = window.setTimeout(loadData, 10000)
}).fadeIn("slow");
}
</script>
</head>
<body>
<div id="load_me"></div>
</body>
</html>
回答by Sanjay
I believe refresh
will work but this will violate time out policy
我相信refresh
会起作用,但这会违反超时政策
回答by Sanjay
The code below worked for me, just try it and let me know if it works for you too.
下面的代码对我有用,试试吧,让我知道它是否也适合你。
<html>
<head>
<Title>Just A Test</Title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function ()
{
$('#load_me').load('samp.jsp').fadeIn("slow");
}, 1000); // autorefresh the content of the div after
//every 1000 milliseconds(1sec)
</script>
</head>
<body>
<div id="load_me"> <%@ include file="samp.jsp" %></div>
</body>
</html>
回答by Able Alias
to load the contents without refresh the page you can use JQuery Ajax for more http://api.jquery.com/category/ajax/
要在不刷新页面的情况下加载内容,您可以使用 JQuery Ajax 获取更多http://api.jquery.com/category/ajax/