在Rails 3.1中每10秒重新加载一次页面 [英] reloading a page after every 10 sec in rails 3.1

查看:57
本文介绍了在Rails 3.1中每10秒重新加载一次页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3.1rc4应用程序,我想每10秒更新一次index.html.erb文件中的div.我不不知道这是否可以通过jquery完成?曾经有一种更新方法,但我不知道它是否仅适用于原型.任何样品都很棒

i have a rails 3.1rc4 app and i would like to update a div in my index.html.erb file after every 10sec. I dont no if this can be done via jquery? there use to be a update method but i dont no if it is for only prototype. any sample would be great

推荐答案

将您的问题分解为子任务,这是您必须处理的问题:

Breaking down your question into sub-tasks, here's what you have to deal with:

  1. 获取(HTTP GET)html
  2. 使用检索到的html更新div
  3. 每10秒重复步骤1和2

对于1,请查看jquery的 .get

For 1, check out jquery's .get

Description: Load data from the server using a HTTP GET request.

对于2,请查看jquery的 .html

For 2, check out jquery's .html

Get the HTML contents ... or set the HTML contents of every matched element.

对于3,请检查 setInterval

Summary
Calls a function or executes a code snippet repeatedly, with a fixed time 
delay between each call to that function. Returns an intervalID.

最终解决方案

HTML代码:

<div id="divToChange"></div>

JavaScript代码

Javascript code

  function update()
  { 
     $.get('/', function(data) 
     {
         $('#divToChange').html(data);
     });
  }
$(document).ready(function($){
  window.setInterval('update()', 10*1000);
});

这篇关于在Rails 3.1中每10秒重新加载一次页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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