質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

解決済

2回答

4632閲覧

カウントダウンタイマーにミリ秒を表示させたい

bakio

総合スコア21

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2020/05/11 01:33

編集2020/05/11 02:14

サイトが表示されてから15分のカウントダウンタイマーを作成したのですが秒の1つ下のミリ秒を表示させたいです。

こちらのサイトをもとに内容を15分にして作成しました。
Simple.Timer 

html

1 <div class="timer" data-minutes-left=15></div>

css

1 2 .timer, 3 .timer-done, 4 .timer-loop { 5 font-size: 12vw; 6 color: #FFF484; 7 font-weight: bold; 8 position: relative; 9 top: 7vw; 10 left: 42vw; 11 width: 100%; 12 } 13 14 15 .jst-hours { 16 float: left; 17 } 18 19 .jst-minutes { 20 float: left; 21 } 22 23 .jst-seconds { 24 float: left; 25 } 26 .jst-milliseconds { 27 float: left; 28 } 29 30 .jst-clearDiv { 31 clear: both; 32 } 33 34 .jst-timeout { 35 color: red; 36 } 37 38 39

jQuery

1 2(function (factory) { 3 // Using as a CommonJS module 4 if(typeof module === "object" && typeof module.exports === "object") { 5 // jQuery must be provided as argument when used 6 // as a CommonJS module. 7 // 8 // For example: 9 // let $ = require("jquery"); 10 // require("jquery-simple-timer")($); 11 module.exports = function(jq) { 12 factory(jq, window, document); 13 } 14 } else { 15 // Using as script tag 16 // 17 // For example: 18 // <script src="jquery.simple.timer.js"></script> 19 factory(jQuery, window, document); 20 } 21}(function($, window, document, undefined) { 22 23 // Polyfill new JS features for older browser 24 Number.isFinite = Number.isFinite || function(value) { 25 return typeof value === 'number' && isFinite(value); 26 } 27 28 var timer; 29 30 var Timer = function(targetElement){ 31 this._options = {}; 32 this.targetElement = targetElement; 33 return this; 34 }; 35 36 Timer.start = function(userOptions, targetElement){ 37 timer = new Timer(targetElement); 38 mergeOptions(timer, userOptions); 39 return timer.start(userOptions); 40 }; 41 42 // Writes to `this._options` object so that other 43 // functions can access it without having to 44 // pass this object as argument multiple times. 45 function mergeOptions(timer, opts) { 46 opts = opts || {}; 47 48 // Element that will be created for hours, minutes, and seconds. 49 timer._options.elementContainer = opts.elementContainer || 'div'; 50 51 var classNames = opts.classNames || {}; 52 53 timer._options.classNameSeconds = classNames.seconds || 'jst-seconds' 54 , timer._options.classNameMinutes = classNames.minutes || 'jst-minutes' 55 , timer._options.classNameHours = classNames.hours || 'jst-hours' 56 , timer._options.classNameClearDiv = classNames.clearDiv || 'jst-clearDiv' 57 , timer._options.classNameTimeout = classNames.timeout || 'jst-timeout'; 58 } 59 60 Timer.prototype.start = function(options) { 61 62 var that = this; 63 64 var createSubElements = function(timerBoxElement){ 65 var seconds = document.createElement(that._options.elementContainer); 66 seconds.className = that._options.classNameSeconds; 67 68 var minutes = document.createElement(that._options.elementContainer); 69 minutes.className = that._options.classNameMinutes; 70 71 var hours = document.createElement(that._options.elementContainer); 72 hours.className = that._options.classNameHours; 73 74 var clearElement = document.createElement(that._options.elementContainer); 75 clearElement.className = that._options.classNameClearDiv; 76 77 return timerBoxElement. 78 append(hours). 79 append(minutes). 80 append(seconds). 81 append(clearElement); 82 }; 83 84 this.targetElement.each(function(_index, timerBox) { 85 var that = this; 86 var timerBoxElement = $(timerBox); 87 var cssClassSnapshot = timerBoxElement.attr('class'); 88 89 timerBoxElement.on('complete', function() { 90 clearInterval(timerBoxElement.intervalId); 91 }); 92 93 timerBoxElement.on('complete', function() { 94 timerBoxElement.onComplete(timerBoxElement); 95 }); 96 97 timerBoxElement.on('complete', function(){ 98 timerBoxElement.addClass(that._options.classNameTimeout); 99 }); 100 101 timerBoxElement.on('complete', function(){ 102 if(options && options.loop === true) { 103 timer.resetTimer(timerBoxElement, options, cssClassSnapshot); 104 } 105 }); 106 107 timerBoxElement.on('pause', function() { 108 clearInterval(timerBoxElement.intervalId); 109 timerBoxElement.paused = true; 110 }); 111 112 timerBoxElement.on('resume', function() { 113 timerBoxElement.paused = false; 114 const secondsLeft = timerBoxElement.data('timeLeft'); 115 const onComplete = timerBoxElement.onComplete; 116 that.startCountdown(timerBoxElement, { secondsLeft, onComplete }); 117 }); 118 119 createSubElements(timerBoxElement); 120 return this.startCountdown(timerBoxElement, options); 121 }.bind(this)); 122 }; 123 124 /** 125 * Resets timer and add css class 'loop' to indicate the timer is in a loop. 126 * $timerBox {jQuery object} - The timer element 127 * options {object} - The options for the timer 128 * css - The original css of the element 129 */ 130 Timer.prototype.resetTimer = function($timerBox, options, css) { 131 var interval = 0; 132 if(options.loopInterval) { 133 interval = parseInt(options.loopInterval, 10) * 1000; 134 } 135 setTimeout(function() { 136 $timerBox.trigger('reset'); 137 $timerBox.attr('class', css + ' loop'); 138 timer.startCountdown($timerBox, options); 139 }, interval); 140 } 141 142 Timer.prototype.fetchSecondsLeft = function(element){ 143 var secondsLeft = element.data('seconds-left'); 144 var minutesLeft = element.data('minutes-left'); 145 146 if(Number.isFinite(secondsLeft)){ 147 return parseInt(secondsLeft, 10); 148 } else if(Number.isFinite(minutesLeft)) { 149 return parseFloat(minutesLeft) * 60; 150 }else { 151 throw 'Missing time data'; 152 } 153 }; 154 155 Timer.prototype.startCountdown = function(element, options) { 156 options = options || {}; 157 158 var intervalId = null; 159 var defaultComplete = function(){ 160 clearInterval(intervalId); 161 return this.clearTimer(element); 162 }.bind(this); 163 164 element.onComplete = options.onComplete || defaultComplete; 165 element.allowPause = options.allowPause || false; 166 if(element.allowPause){ 167 element.on('click', function() { 168 if(element.paused){ 169 element.trigger('resume'); 170 }else{ 171 element.trigger('pause'); 172 } 173 }); 174 } 175 176 var secondsLeft = options.secondsLeft || this.fetchSecondsLeft(element); 177 178 var refreshRate = options.refreshRate || 1000; 179 var endTime = secondsLeft + this.currentTime(); 180 var timeLeft = endTime - this.currentTime(); 181 182 this.setFinalValue(this.formatTimeLeft(timeLeft), element); 183 184 intervalId = setInterval((function() { 185 timeLeft = endTime - this.currentTime(); 186 // When timer has been idle and only resumed past timeout, 187 // then we immediatelly complete the timer. 188 if(timeLeft < 0 ){ 189 timeLeft = 0; 190 } 191 element.data('timeLeft', timeLeft); 192 this.setFinalValue(this.formatTimeLeft(timeLeft), element); 193 }.bind(this)), refreshRate); 194 195 element.intervalId = intervalId; 196 }; 197 198 Timer.prototype.clearTimer = function(element){ 199 element.find('.jst-seconds').text('00'); 200 element.find('.jst-minutes').text('00:'); 201 element.find('.jst-hours').text('00:'); 202 }; 203 204 Timer.prototype.currentTime = function() { 205 return Math.round((new Date()).getTime() / 1000); 206 }; 207 208 Timer.prototype.formatTimeLeft = function(timeLeft) { 209 210 var lpad = function(n, width) { 211 width = width || 2; 212 n = n + ''; 213 214 var padded = null; 215 216 if (n.length >= width) { 217 padded = n; 218 } else { 219 padded = Array(width - n.length + 1).join(0) + n; 220 } 221 222 return padded; 223 }; 224 225 var hours = Math.floor(timeLeft / 3600); 226 timeLeft -= hours * 3600; 227 228 var minutes = Math.floor(timeLeft / 60); 229 timeLeft -= minutes * 60; 230 231 var seconds = parseInt(timeLeft % 60, 10); 232 233 if (+hours === 0 && +minutes === 0 && +seconds === 0) { 234 return []; 235 } else { 236 return [lpad(hours), lpad(minutes), lpad(seconds)]; 237 } 238 }; 239 240 Timer.prototype.setFinalValue = function(finalValues, element) { 241 242 if(finalValues.length === 0){ 243 this.clearTimer(element); 244 element.trigger('complete'); 245 return false; 246 } 247 248 element.find('.' + this._options.classNameSeconds).text(finalValues.pop()); 249 element.find('.' + this._options.classNameMinutes).text(finalValues.pop() + ':'); 250 element.find('.' + this._options.classNameHours).text(finalValues.pop() + ':'); 251 }; 252 253 254 $.fn.startTimer = function(options) { 255 this.TimerObject = Timer; 256 Timer.start(options, this); 257 return this; 258 }; 259 260})); 261

この状態だと一番小さい単位だと秒まではわかるんですがその下のミリ秒が表示されず、
millisecond などのワードを追加しても表示されませんでした。

こちらわかる方いらっしゃいましたら助けて欲しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kei344

2020/05/11 02:01

提示されたコードの出典(公式URL)を質問文に追記してください。
kei344

2020/05/11 02:06

この「質問への追記・修正の依頼」の部分はデフォルトで表示されませんので、質問本文に追記することをお勧めします。
bakio

2020/05/11 02:11

ご教示いただきありがとうございます。そう致します。
kei344

2020/05/11 02:30

そもそもミリ秒を表示する機能の無いコードですが、出典のコードから何を変更されたのでしょう。
bakio

2020/05/11 02:33

javascriptは変更していません。 変更したのはHTML内の data-minutes-left=15 の15という数字とCSSのレイアウト程度です。 こちらのコードではそもそもミリ秒は不可能なんでしょうか。
kei344

2020/05/11 02:36

> こちらのコードではそもそもミリ秒は不可能なんでしょうか。 はい、このコードはミリ秒を表示するようにはできていません。少しの改造程度ではミリ秒を表示することは難しいです。
bakio

2020/05/11 02:37

かしこまりました。別の方法でやってみます
guest

回答2

0

自己解決

javascriptを以下の指示にしたら成功しました。

javascript

1 2 <script type="text/javascript"> 3 window.addEventListener( 4 "load", 5 function() { 6 var cdown = new Object(); 7 cdown.elem = document.querySelectorAll(".cdown"); 8 cdown.tmr = new Array(); 9 cdown.getTime = function(I) { 10 var attributes = cdown.elem[I].attributes; 11 for (var type, value, i = 0; i < attributes.length; i++) { 12 type = attributes[i].name.match(/(^data-cdown-)(.+)/); 13 if (type) { 14 value = parseInt(attributes[i].value, 10) || 0; 15 return type[2] == "min" ? value * 60 : value; 16 } 17 } 18 return null; 19 }; 20 cdown.start = function(I) { 21 var sec = cdown.getTime(I); 22 if (sec == null) { 23 return; 24 } 25 var target = new Date().getTime() + sec * 1000; 26 cdown.tmr[I] = setInterval(function() { 27 var now = new Date().getTime(); 28 var left = target - now; 29 var output = { 30 min: Math.floor((left % (1000 * 60 * 60)) / (1000 * 60)), 31 sec: Math.floor((left % (1000 * 60)) / 1000), 32 ms: left % 1000, 33 }; 34 console.log(left); 35 if (left > 0) { 36 cdown.elem[I].textContent = "" + ("0" + output.min).slice(-2) + " 分 " + ("0" + output.sec).slice(-2) + " 秒 " + ("00" + output.ms).slice(-2); 37 } else { 38 cdown.elem[I].textContent = "0:00"; // 終了後に表示される言葉 39 window.clearInterval(cdown.tmr[I]); 40 } 41 }, 20); 42 }; 43 for (var i = 0; i < cdown.elem.length; i++) { 44 cdown.start(i); 45 } 46 }, 47 false 48 ); 49 </script>

投稿2020/05/18 07:29

bakio

総合スコア21

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

javascript

1console.log(new Date().getTime()%1000);

投稿2020/05/11 02:23

yambejp

総合スコア114769

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bakio

2020/05/11 02:28 編集

yambe.jpさん こちらをどこに入れればいいでしょうか? 具体的に教えていただくことは可能でしょうか 試しに今やってますがエラー表示になります...
yambejp

2020/05/11 02:30

逆に質問者参賀どこにどう入れたいのかわからないのでなんとも・・・
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問