Ruby on Rails4でタイマーを開発しています。
jQueryで時間の計測処理を行い、Rails上でテーブルの更新を行いたいのですが上手く実装ができません。
こちらはドットインストールで紹介されているソースコードですが、JavaScriptのstarttimeとstoptimeをRailsのtimeテーブルのstoptimeとstarttimeというカラム上に格納したいです。
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>ストップウォッチ</title> 6</head> 7<body> 8 <h1>ストップウォッチ</h1> 9 <div id="sec" style="font-size:128px">0.00</div> 10 <input type="button" value="Start!" id="run"> 11 <input type="button" value="Stop!" id="stop"> 12 <input type="button" value="Reset!" id="reset">
JavaScript
1 2 <script> 3 (function() { 4 var startTime, 5 stopTime, 6 running = false, 7 timerId; 8 9 document.getElementById('run').onclick = function() { 10 run(); 11 } 12 13 document.getElementById('stop').onclick = function() { 14 stop(); 15 } 16 17 document.getElementById('reset').onclick = function() { 18 reset(); 19 } 20 21 function run() { 22 23 if (running) return; 24 25 running = true; 26 27 if (stopTime) { 28 startTime = startTime + (new Date()).getTime() - stopTime; 29 } 30 31 if (!startTime) { 32 startTime = (new Date()).getTime(); 33 } 34 35 timer(); 36 } 37 38 function timer() { 39 document.getElementById('sec').innerHTML = (((new Date()).getTime() - startTime) / 1000).toFixed(2); 40 timerId = setTimeout(function() { 41 timer(); 42 }, 100); 43 } 44 45 function stop() { 46 if (!running) return false; 47 running = false; 48 clearTimeout(timerId); 49 stopTime = (new Date()).getTime(); 50 } 51 52 function reset() { 53 if (running) return; 54 startTime = undefined; 55 document.getElementById('sec').innerHTML = '0.00'; 56 } 57 58 })(); 59 </script> 60</body> 61</html>
ご教授いただければ幸いです。
よろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。