サイトが表示されてから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 などのワードを追加しても表示されませんでした。
こちらわかる方いらっしゃいましたら助けて欲しいです。
回答2件
あなたの回答
tips
プレビュー