使用 JQuery 自动刷新 DIV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17683227/
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 Refresh DIV using JQuery
提问by ronan
My requirement is to auto refresh a DIV every 5 seconds
我的要求是每 5 秒自动刷新一个 DIV
The DIV content I want to refresh is
我要刷新的DIV内容是
<div class="row-fluid">
<div class ="span2">
<label><spring:message code='total.registration' />:</label>
</div>
<div class = "span3">
${registrationStatusForm.totalRegis}
</div>
</div>
I also checked some questions on stackoverflow but didn't understand. Please note that I'm using Spring Web MVC. Please suggest.
我也查了一些关于stackoverflow的问题,但没看懂。请注意,我使用的是 Spring Web MVC。请建议。
回答by Brad
This is a jquery question not a spring question since the refresh will be managed on the client.
这是一个 jquery 问题而不是一个 spring 问题,因为刷新将在客户端上进行管理。
In jquery, something like this would be appropriate:
在 jquery 中,这样的事情是合适的:
$(document).ready(function(){
setInterval(refreshDiv, 5000);
});
function refreshDiv(){
$.ajax({
url: "http://path.to.your/webservice",
//other stuff you need to build your ajax request
}).done(function() {
//update your div
});
}
回答by Mahesh Gosemath
You need to create a new view and controller with minimum elements required to be refreshed.
您需要创建一个新的视图和控制器,其中需要刷新的元素最少。
I also faced the same problem and I fixed it by writing a new controller and a view for the div to be refreshed and then used setInterval, actually setTimeout worked better for my requirement. :
我也遇到了同样的问题,我通过编写一个新的控制器和一个视图来修复它来刷新 div,然后使用 setInterval,实际上 setTimeout 更适合我的要求。:
setInterval(function(){
$('#your_div').load('newController');
}, time_interval);
回答by A.O.
setInterval(function(){
//code here to refresh div
//possibly: document.getElementById("idOfDiv").innerHTML = "new content";
}, 5000);