如何使用Ajax频繁地重新加载部分代码? [英] How can I reload partial frequently using Ajax?

查看:84
本文介绍了如何使用Ajax频繁地重新加载部分代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在后台,我希望它重新加载并显示有多少未读邮件.
我想要没有刷新的页面.我的意思是使用ajax.

In background, I want it to reload and shows the number how many unread messages are there.
I want that without refreshing page. I mean using ajax.

如果我在菜单中有此菜单,如何每30秒仅刷新一次此部分?

If I had this in menu, how can I refresh only this section every 30 secs?

<li><%= link_to sanitize('<i class="icon-envelope"></i> ') + "received messages" + sanitize(' <span class="badge badge-info">'+current_user.mailbox.inbox(:read => false).count(:id, :distinct => true).to_s+'</span>'), messages_received_path  %></li>

messages_controller.rb

messages_controller.rb

  def received
    if params[:search]
     @messages = current_user.mailbox.inbox.search_messages(@search).page(params[:page]).per(10)
    else
     @messages = current_user.mailbox.inbox.page(params[:page]).per(10)
    end 
     add_crumb 'Messages Received', messages_received_path

     @box = 'inbox'
     render :index
  end

推荐答案

类似的方法应该起作用:

Something like this should work:

控制器

def messages_received
  @messages = Messages.messages_received
  respond_to |format|
   format.json { render json: @messages }
  end
end  

js

 setInterval(function(){
   $.ajax({
        type: 'GET',
        url: '/messages_received',
        data: {},
        success: function(JSONRESPONSE){
            alert(JSONRESPONSE)  // @messages variable
        };
   })
 },30000)   // 30 segs

这篇关于如何使用Ajax频繁地重新加载部分代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆