简体   繁体   English

当计时器在 60 秒后启动时显示分钟,然后显示小时

[英]When timer starts after 60 seconds show minutes and then hours

When the start task button is clicked I want an invisible timer to start, and then when the finished Task button is clicked I want the time it took to finish the task to be displayed.单击开始任务按钮时,我希望启动一个不可见的计时器,然后单击完成的任务按钮时,我希望显示完成任务所需的时间。 After 60 seconds I want the time to be displayed in in minutes and then after 60 minutes I want the time to hours. 60 秒后,我希望时间以分钟为单位显示,然后在 60 分钟后,我希望时间以小时为单位。

Right now when you fun my code it will show the time but it only shows the time in seconds.现在,当您玩弄我的代码时,它会显示时间,但它仅以秒为单位显示时间。

let startTime;

const timer = typeof performance !== `undefined` && typeof performance.now === `function` ? performance : Date;
const startButton = document.getElementById('start');
const stopButton = document.getElementById('stop');
const display = document.getElementById('display');

startButton.onclick = () => {
    console.debug('START')
    startTime = timer.now();
};

stopButton.onclick = () => {
    console.debug('STOP')
    display.innerHTML = Math.round((timer.now() - startTime) / 1000);
};

<h1>
      <!-- This shows the heading of the entire checklist -->
        Master on Call Checklist
    </h1>

    <ul class="checklist ng-pristine ng-untouched ng-valid ng-isolate-scope ui-sortable" ui-sortable="sortableOptions" ng-model="task">

        <li> <!-- This puts a bullet point in front of the title-->
          <h2>            
            <!-- This shows the heading of task done after hours -->
            <a href="#"> <!-- This makes the title blue -->
              Done after regular work hours</a>
          </h2>
        </li>

        <li>  
          <h2>
            <!-- This shows the heading of task done during regular hours -->
            <a href="#">
              Done during regular work hours
            </a>
          </h2>
        </li>

        <li>
          <h2>
            <!-- This shows the heading of task that need to be constantly looked at -->
            <a href="#">
              Tasks that need to be constantly checked throughout the week
            </a>
          </h2>
        </li>

        <button type="button" id="start">Start Task</button>
          <p style="float:left;"></p>
            <!-- Heading to review cameras and adjest as needed -->
          <a>
            Review cameras and adjest as needed
          </a>
        <button type="button" id="stop">Finished Task</button>
        <div id="display"></div>

Try this:尝试这个:

 stopButton.onclick = () => { console.debug('STOP'); var time = Math.round((timer.now() - startTime) / 1000); if (time >= 60) { >= 1 minute time = time / 60; // Minutes if (time >= 60) { // >= 1 hour time = time / 60; // Hours } } display.innerHTML = time; };

The minutes and hours will be displayed as decimals unless you round (or floor) those as well.分钟和小时将显示为小数,除非您也舍入(或取舍)它们。 I assume, however, that you want it displayed more like "1:15" or "2:20:15" instead, so this will take care of that:但是,我假设您希望它显示得更像“1:15”或“2:20:15”,所以这会解决这个问题:

 stopButton.onclick = () => { console.debug('STOP'); var totalSeconds = Math.round((timer.now() - startTime) / 1000); var totalMinutes = Math.floor(totalSeconds / 60); var totalHours = Math.floor(totalSeconds / 60 / 60); var displaySeconds = totalSeconds - totalMinutes * 60; // Gets the number of seconds to display var displayMinutes = totalMinutes - totalHours * 60; // Gets the number of minutes to display var strDisplayTime = (totalHours > 0 ? (totalHours + ':') : '') + (displayMinutes > 0 || totalHours > 0 ? ((displayMinutes >= 10 ? '' : '0') + displayMinutes + ':') : '') + ((displaySeconds >= 10 ? '' : '0') + displaySeconds) display.innerHTML = strDisplayTime; };

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

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