Javascript 获取当前时间(时间戳)并保存到数据库
我正在跟踪用户在会话中花费的时间。我所做的是在他们进入会话时创建一个令牌,并跟踪该令牌是何时创建的 这意味着我基本上有一个起点,现在我希望能够跟踪终点,也就是他们完成一个会话的时候 幸运的是,我已经有了一个事件跟踪器,可以跟踪他们何时从会话断开连接,但是我现在想要完成的是,当该跟踪器被激活时,然后存储结束Javascript 获取当前时间(时间戳)并保存到数据库,javascript,ruby-on-rails,Javascript,Ruby On Rails,我正在跟踪用户在会话中花费的时间。我所做的是在他们进入会话时创建一个令牌,并跟踪该令牌是何时创建的 这意味着我基本上有一个起点,现在我希望能够跟踪终点,也就是他们完成一个会话的时候 幸运的是,我已经有了一个事件跟踪器,可以跟踪他们何时从会话断开连接,但是我现在想要完成的是,当该跟踪器被激活时,然后存储结束 session.on 'sessionDisconnected', (event) -> console.log 'You were disconnected from the se
session.on 'sessionDisconnected', (event) ->
console.log 'You were disconnected from the session' + event.reason
# track that session ended in database
if event.reason == 'networkDisconnected'
alert 'Your network connection terminated.'
else if event.reason == 'clientDisconnected'
console.log 'You closed the session'
return
基本上,我的评论是我想发送请求在数据库中存储时间的地方
我想将时间戳保存在表“customers”和名为“finished_at”的列中
那么,我如何获取当前时间并将其作为时间戳保存到我的客户中。我的问题是完成时间。对于JavaScript中的当前时间,请使用new Date()并从中获取时间戳
var timeStamp=new Date().getTime() 首先,您必须创建一个服务器入口点,用于更新
finished\u at
字段。例如:
routes.rb
:
resources :customers do
get :finish
end
class CustomersController < ApplicationController
def finish
customer = Customer.find(params[:customer_id])
if customer.update(finished_at: Time.now)
some_action
else
another_action
end
end
end
customers\u controller.rb
:
resources :customers do
get :finish
end
class CustomersController < ApplicationController
def finish
customer = Customer.find(params[:customer_id])
if customer.update(finished_at: Time.now)
some_action
else
another_action
end
end
end
(根据其他人的说法,使用GET-here不是一个好主意。它只是为了展示主要思想)
如果需要,请随时询问详细信息。您如何与服务器通信?如果您让restAPI/server调用将此时间戳作为有效负载发送,那么在服务器上这样做要可靠得多,您无法确保客户端的时钟设置正确。