如何定期获取在轨/更新的价值?价值

2023-09-10 16:38:00 作者:心已碎ㄨ谁来慰

让我们一起来情景:

 柜台10秒
 

在用户访问 show.html.erb show.html.erb 获取从数据库使用&LT的价值;%= @ post.value%> 计数器开始计数和每个迭代是10秒。 在每个10秒我想改变 @ post.value 使用工具后类。 更新 @ post.value 在数据库中。 刷新 show.html.erb 自动<%= @ p​​ost.value%> 将显示更新的值 在上述过程将在循环中运行,直到用户移动到其他页面。 升级Win10系统卡在正在获取更新怎么办

如果我必须简化code中的问题,那么这将是这样的:

查看页面

 <%= @ p​​ost.value%>
&其中;%= @ p​​ost.name%GT;
 

控制器方法

 高清节目
 @post = Post.find(PARAMS [:ID])
结束

DEF更新
....#这是空的,现在
结束

高清fetching_value_from_PostUpdate(current_value)
 ..#现在我需要更新数据库中的值
结束
 

工具类

我要更新 post.value 方法写在这个类的基础上。伪code:

 类PostUpdate
 @@ instance_variable

 高清初始化(post_value)
   @@ instance_variable = Marshal.load(Marshal.dump(post_value))
 结束

  #Method需要计算的值
高清update_data_in_database
 ...
返回数据
结束
 

问题

在哪里我必须把柜台?客户端或服务器端?我不想使用后台作业的轨道。 在什么样的变化,我需要做的显示方法,这样以后每间隔页面会自动刷新,并通过在 @post更新值。值

感谢您的帮助!

解决方案

我将与火力地堡去,而不是到轮询服务器。

不过,如果你想定期轮询服务器,我只想有一个它执行每隔10秒,或在任何时间你想JavaScript方法,并获取任何数据,你想异步并随后更新DOM的。

另外,红宝石包装火力API 时,在的情况下可能会感兴趣

Let's take a scenario:

counter 10 seconds

User visited show.html.erb page show.html.erb fetch the value from database using <%= @post.value %>. Counter started and each iteration of counter is for 10 seconds. After every 10 seconds I wanted to change the @post.value using utility class. update the @post.value in database. Refresh the show.html.erb automatically and <%= @post.value %> will show the updated value Above process will be run in loop until user move to other pages.

If I have to simplify the problem in code then it would like this:

View Page

<%= @post.value %>
<%= @post.name %>

Controller Method

def show
 @post = Post.find(params[:id])
end

def update
....   #It's empty right now
end

def fetching_value_from_PostUpdate(current_value)
 .. # Now I need to update the value in database
end

Utility class

I want to update the post.value on the basis of method written in this class. Pseudo code:

class PostUpdate
 @@instance_variable   

 def initialize(post_value)
   @@instance_variable = Marshal.load(Marshal.dump(post_value))
 end

  #Method required to calculate the value
def update_data_in_database
 ...
return data
end

Questions

Where do I have to put the counter? client side or server side? I don't want to use background jobs in rails. What changes do I need to make in show method so that after every interval page will refresh automatically and pass the updated value in @post.value?

Thanks for your help!

解决方案

I would go with Firebase as opposed to polling the server.

However, if you're wanting to poll the server periodically, I would just have a JavaScript method which executes every 10 seconds, or at whatever interval you'd like, and fetches whatever data you'd like asynchronously and subsequently updates the DOM.

Also, ruby wrapper for firebase api, in case that may be of interest