フリー学習サイトを参考にタイマーを作っているのですが作動しない原因がわかりません。綴りミスとかでしょうか?
startを押してstopを押したら経過時間が表示されるようにしたいです。
何卒ご教授お願いします。
javascript
1(function(){ 2 'use strict'; 3 4 var start = document.getElementById('start'); 5 var stop = document.getElementById('stop'); 6 var result = document.getElementById('result'); 7 var startTime; 8 9 document.addEventListener("DOMContentLoaded", function(){ 10 start.addEventListener('click',function(){ 11 startTime = Date.now(); 12 }); 13 }); 14 document.addEventListener("DOMContentLoaded", function(){ 15 stop.addEventListener('click', function() { 16 var elapsedTime; 17 18 elapsedTime = (Date.now()-startTime)/1000; 19 result.textContent=elapsedTime.toFixed(3); 20 }); 21 }); 22})();
html
1<!DOCTYPE html> 2<html> 3<head> 4 <meta charset="utf-8" /> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 6 <title>5 second</title> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <link rel="stylesheet" type="text/css" media="screen" href="css/styles.css"/> 9 <script src="js/main.js"></script> 10</head> 11<body> 12 <div class ="container"> 13 <div id="target">5.000</div> 14 <div id="result">0.000</div> 15 <div id="buttons"> 16 <div id="start">start</div> 17 <div id="stop">stop</div> 18 </div> 19 </div> 20 21</body> 22</html>
css
1.container{ 2 font-family: 'Courier New', sans-serif; 3 width: 300px; 4 margin: 30px auto 0; 5 text-align: center; 6 font-weight: bold; 7} 8 9#target, #result{ 10 font-size: 32px; 11 margin-bottom: 15px; 12 background: #ccc; 13 height: 60px; 14 line-height: 60px; 15} 16 17#start{ 18 float:left; 19} 20 21#stop{ 22 float:right; 23} 24 25#start,#stop{ 26 cursor:pointer; 27 font-size: 18px; 28 width:145px; 29 background: #eee; 30 height: 40px; 31 line-height: 40px; 32 box-shadow: 0 6px 0 #ccc; 33}
OSはWindows
環境はchrome,Edgeなどで実行していますが、「 start.addEventListener('click', function() {」この行で同じエラーが出ます。
開発ツールはVisual Studio Codeを使用しています。
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/16 14:18
2018/04/16 14:24
2018/04/17 00:57
2018/04/17 01:37
2018/04/17 03:08
2018/04/17 05:14
2018/04/17 12:30
2018/04/17 12:34
2018/04/17 13:09
2018/04/17 13:20 編集
2018/04/18 01:33