简体   繁体   中英

Timer - javascript

I have a problem with a timer in below function. How to change seconds for a minute and seconds? In present function, the timer counting only seconds and milliseconds.

if (timer === 1) {
    var startTime = Date.now();
    the_timer = setInterval(function() {
        var elapsedTime = Date.now() - startTime;
        finished_time = (elapsedTime / 1000).toFixed(3);
        $("#timer").text(finished_time);
    }, 44);

}

The function getTimeStr does this. It even formats it nicely so 3 seconds will be displayed as 0:03 , 32 as 0:32 and 71 seconds as 1:11 .

 function getTimeStr(milliseconds) { var minutes = milliseconds / 60000; var intMinutes = Math.floor(minutes); var seconds = Math.floor((minutes - intMinutes) * 60); return intMinutes + ':' + (seconds < 10 ? ('0' + seconds.toFixed(0)) : seconds.toFixed(0)); } var startTime = new Date(); setInterval(function() { var elapsedTime = Date.now() - startTime; console.log(getTimeStr(elapsedTime)); }, 100); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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