こんにちは私はRailsの初心者で、Rubyで数日経験しています。スレッドを使用してストップウォッチプログラムを作成しました。しかし今、私はそれをRailsアプリ内に置きたいので、ビューには、イベントを追跡しているストップウォッチcozを開始、停止、再開するためのボタンが必要です。誰かがルビープログラムをレールに埋め込んで、それをビューから使用する方法を示すことができますか?
以下のプログラムは、ビューの開始/停止/再開ボタンから呼び出す必要があります
stopwatch.rb :(現時点ではstartメソッドとpauseメソッドがあります)
class Stopwatch
def initialize
@t_start
@t_elapsed
@t_total
@t_current
end
def start
@t_start = Time.now
t = Thread.new do
while true do
@t_current = Time.now
puts @t_current
@t_elapsed = @t_current - @t_start
puts "Time elapsed is : #{@t_elapsed.to_i} seconds"
sleep 1
end
end
end
def pause
# Stop the Thread => Sleep the Thread until Resume is pressed.
t.kill
# Calculate the elapsed time
@t_elapsed = @t_current - @t_start
end
end
ticker = Stopwatch.new
ticker.start
ありがとう、