Rubytastic
Rubytastic

Reputation: 15491

Realtime timer that counts down time for ruby on rails app

Im in the need on some advise for a realtime serverside timer that would countdown from say 100 seconds. Reason for serverside timer is no tampering.

Anyone could advise to get a realtime counter in rails app serverside? I would want to create one or more independant timers i can get a value from in railsapp.

Upvotes: 2

Views: 1639

Answers (1)

tadman
tadman

Reputation: 211540

Why don't you just create the record you need, but add a "don't open before" DateTime field you can check to see if it's okay to show it?

This sort of thing is trivial to do, and you can set a timer on the client to count-down in JavaScript, then reload the page with the final data at the appropriate time. If someone is impatient and reloads early, you can compute the number of seconds remaining before it can be shown using simple math:

time_left_in_seconds = record.show_at.to_i - Time.now.to_i

Then all you have to do is show a JavaScript timer for that number of seconds, then trigger a page refresh.

Upvotes: 1

Related Questions