打印

radrails中能否做到根据时间进行AJAX刷新,急啊!!!!

radrails中能否做到根据时间进行AJAX刷新,急啊!!!!

还没碰到过有关时间方面的情况!!!
EG: 时间间隔 20S 更新内容 一段文字(画面中的一个table中) 要用ajax刷新!!
帮忙!!!!!!谢谢!!!!!

TOP

找到一个类 Timer

代码如下:

#
# timer.rb
#
#   Copyright (c) 1999-2002 Minero Aoki <aamine@loveruby.net>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU Lesser General Public License version 2 or later.
#
#   $Id: timer.rb,v 1.8 2002/01/05 06:46:49 aamine Exp $
#
# Usage:
#
#   t = Timer.new(10) { raise TimeoutError, "timeout!" }
#   t.start
#     :      # done with 10sec timeout
#   t.stop
#
#   t.start
#     :
#   if condition then
#     t.reset       #--> restart timer
#   end
#

require 'timeout'    # for TimeoutError


class Timer

  def initialize( s, &block )
    @sec = s
    @on_timeout = block
    @current = nil
    @timer_thread = nil
  end

  attr_accessor :sec

  def on_timeout( &block )
    if block then
      @on_timeout = block
      true
    else
      false
    end
  end

  def start
    @current = Thread.current
    @timer_thread = Thread.fork {
        sleep @sec
        if @on_timeout then
          @on_timeout.call @sec
        else
          @current.raise TimeoutError "#{@sec} seconds past"
        end
    }
    if iterator? then
      begin
        yield
      ensure
        stop
      end
    else
      @sec
    end
  end

  def stop
    if @timer_thread then
      Thread.kill @timer_thread
      @timer_thread = nil
      true
    else
      false
    end
  end

  def reset
    stop
    start
  end

end


class DammyTimer < Timer

  def start
    if iterator? then
      yield
    else
      sec()
    end
  end

  def stop
    false
  end

end


但是我进行初始化的报错了(cook.rb)
t_proc = proc do |sender|
p "put out "
end
 t = Timer.new  t.on_timer = t_proc #这句话有问题
 t.interval = 1000  

报错信息:
cook.rb:106:in `initialize': wrong number of arguments (0 for 1) (ArgumentError)

TOP

不是很明白你要什么,因为:

1. Radrails 是集成开发环境,我假设你要的是Rails程序产生的网页中定期更新

2. 你找到的 timer.rb 只能在服务端执行,和客户端的页面更新没关系

如果你是需要浏览器中的页面内容定期更新,那么可以使用Rails 自带的 Prototype JavaScript 包含一个Ajax定期更新器 Ajax.PeriodicalUpdater

使用很简单:

比如你的页面里面有如下html元素
<div id="items">
...
</div>

然后你的Rails程序在 /itmes地址提供更新内容,那么:

new Ajax.PeriodicalUpdater('items', '/items', {
 method: 'get', frequency: 3, decay: 2
});

具体文挡在
http://www.prototypejs.org/api/ajax/periodicalUpdater

TOP

xiexie 。。

你的方法很好。。。已经实现了

TOP

2008-11-24 00:25 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61