简体   繁体   English

JavaScript时间量

[英]JavaScript Amount Of Time

I'm currently working on a project that will display the amount of time left in a certain time period based on the users current time. 我目前正在开发一个项目,该项目将根据用户的当前时间显示特定时间段内剩余的时间。 Here is the code. 这是代码。

 var periods = [ [ '07:45' , '08:34' ], [ '08:38' , '09:30' ], [ '09:34' , '10:23' ], [ '10:27' , '11:16' ], [ '11:20' , '12:38' ], [ '12:42' , '15:55' ], [ '07:00' , ] ]; updateTimePeriods(); setInterval(updateTimePeriods, 1000); // Update every second function updateTimePeriods() { var listEl = document.getElementById('periods'); var now = new Date(); var count = periods.length; listEl.innerHTML=''; for (var i = 0; i < count; i++) { if(formatTimeRemaining(timeLeft(now, periods[i][1])).charAt(0)!='–') { child=listEl.appendChild(document.createElement('LI')); child.innerHTML = periods[i][0] + ' — ' + periods[i][1] + ' => Duration: ' + formatUTCTime(duration(periods[i][0], periods[i][1])) + ', Remaining: ' + formatTimeRemaining(timeLeft(now, periods[i][1])); } } } function duration(start, end) { var startTime = parseTime(start); var endTime = parseTime(end); return endTime.getTime() - startTime.getTime(); } function timeLeft(now, end) { var nowTime = parseTime(formatTime(now)); var endTime = parseTime(end); return endTime.getTime() - nowTime.getTime(); } function parseTime(timeStr) { var tokens = timeStr.split(':'); return new Date(1970, 0, 1, parseInt(tokens[0], 10), parseInt(tokens[1], 10)); } function formatUTCTime(time) { var date = new Date(time); return padZero(date.getUTCHours()) + ':' + padZero(date.getUTCMinutes()); } function formatTime(time) { var date = new Date(time); return padZero(date.getHours()) + ':' + padZero(date.getMinutes()); } function formatTimeRemaining(time) { var sign = '+'; if (time < 0) { time *= -1; sign = '–'; } var date = new Date(time); return sign + padZero(date.getUTCHours()) + ':' + padZero(date.getUTCMinutes()) + ':' + padZero(date.getUTCSeconds()); } function padZero(n) { return ('00' + n).substr(-2); } 
 body { background-color: #A00000; background-size: cover; margin: 0; padding: 0; } .outer-box { border: 3px solid black; height: true; width: 75%; padding: 10px; margin: 10px auto 10px auto; border-radius: 10px; background-color: white; text-align:center; } #periods { border-radius: 5px; margin: 20px auto 20px auto; padding: 5px; font-weight: bold; text-align: center; list-style-type: none; } 
 <div class="outer-box"> <ul id="periods"></ul> </div> 

My goal is only display to the user the amount of time left in the current time period instead of all of them. 我的目标是仅向用户显示当前时间段内剩余的时间,而不是全部显示给用户。 And if its in between a time period it shows the amount of time until the next one occurs. 并且如果它在一个时间段之间,则显示直到下一个时间间隔发生的时间量。 The issue is once all the times occur I need to tell him much time until the start of the next time which occurs on a different day. 问题是所有时间都发生了,我需要告诉他很多时间,直到下一次在不同的日子发生。 To elaborate, currently if all the time periods occur it displays a blank space because there is nothing to display and all the times are negative. 详细地说,当前如果所有时间段都发生,则显示空白,因为没有内容可显示且所有时间均为负。 I want to display the amount of time until the next days starting time. 我想显示直到第二天开始的时间。

You should break the for loop as you create the new LI element. 创建新的LI元素时,应中断for循环。 This way it should only show the actual time period. 这样,它应该只显示实际时间段。

for (var i = 0; i < count; i++) {
 if(formatTimeRemaining(timeLeft(now, periods[i][1])).charAt(0)!='–') {
  child=listEl.appendChild(document.createElement('LI'));
  child.innerHTML = periods[i][0] + ' — ' + periods[i][1]
     + ' => Duration: ' + formatUTCTime(duration(periods[i][0], periods[i][1]))
     + ', Remaining: ' + formatTimeRemaining(timeLeft(now, periods[i][1]));
  break;  
 }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM