【问题标题】:Auto Refresh DIV using JQuery使用 JQuery 自动刷新 DIV
【发布时间】:2013-07-16 17:27:45
【问题描述】:

我的要求是每 5 秒自动刷新一次 DIV

我要刷新的DIV内容是

<div class="row-fluid">
    <div class ="span2">
        <label><spring:message code='total.registration' />:</label>
    </div>
    <div class = "span3">
        ${registrationStatusForm.totalRegis}
    </div>
</div>

我还查了一些关于 stackoverflow 的问题,但不明白。 请注意,我使用的是 Spring Web MVC。 请提出建议。

【问题讨论】:

  • 看看 setInterval()

标签: javascript jquery ajax


【解决方案1】:

这是一个 jquery 问题而不是 spring 问题,因为刷新将在客户端上进行管理。

在 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
    });
}

【讨论】:

  • 假设我想刷新registration.jsp页面的DIV结果,请告诉我是否可以使用url作为registration.jsp,它有div代码来显示函数totalRegisRefresh() { $ .ajax({ url : 'registration.jsp', 成功: function(data) { $('#result').jsp(data); } }); }
  • 那个jsp页面是返回一个完整的dom还是一个dom片段?
  • 那么没有。您正在更新一个 dom 片段而不是整个 dom。最好的方法是修改这个特定部分的服务器端代码以返回模型或 dom 片段。
  • 有什么办法吗?
  • 我在看crunchify.com/… 的一个例子现在这里在Ajax 调用中传递的URL 是ajaxtest.html 我没明白,其中页面是ajax.jsp 是否意味着我需要编写ajax。 html 另一个页面来显示结果?
【解决方案2】:

您需要创建一个新的视图和控制器,其中需要刷新的元素最少。

我也遇到了同样的问题,我通过编写一个新的控制器和一个要刷新的 div 视图来修复它,然后使用 setInterval,实际上 setTimeout 更适合我的要求。 :

setInterval(function(){
    $('#your_div').load('newController');
}, time_interval);

【讨论】:

    【解决方案3】:
    setInterval(function(){
    //code here to refresh div
    //possibly: document.getElementById("idOfDiv").innerHTML = "new content";
    }, 5000);
    

    【讨论】:

      猜你喜欢
      • 2010-10-05
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2012-12-01
      • 2012-10-11
      • 2012-04-20
      相关资源
      最近更新 更多