Help to remake the script so that the result is displayed as HOUR: MINUTES: SEC

var TIMEOUT = 15; // sec function showTime(t) { document.getElementById("time").innerHTML = (t != 0) ? t : ""; } function timer(t) { document.getElementById("btn").disabled = true; if (t == undefined) t = TIMEOUT; showTime(t); var interval = setInterval(function() { --t; showTime(t); if (t <= 0) { clearInterval(interval); document.getElementById("btn").disabled = false; } }, 1000); } function onClick() { document.cookie = "timeout=" + new Date(); timer(); } function getCookie(name) { var matches = document.cookie.match( new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)") ); return matches ? decodeURIComponent(matches[1]) : undefined; } function onLoad() { var prevStartTime = new Date(getCookie("timeout")); var delta = TIMEOUT - Math.round((new Date() - prevStartTime) / 1000); if (delta > 0) { timer(delta); } } 
 <body onload="onLoad();"> <button id="btn" onclick="onClick();">Click me!</button> <br /> <span id="time"></span> </body> 

Closed due to the fact that the essence of the question is unclear by the participants Mikhail Vaysman , Akina , Denis Bubnov , user194374, Vadim Ovchinnikov January 31, '17 at 10:36 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • what script needs to be remade? - Grundy
  • I can’t add an hour and a minute to this script - Ilmir Murtazin

1 answer 1

 String.prototype.toHHMMSS = function () { var sec_num = parseInt(this, 10); var hours = Math.floor(sec_num / 3600); var minutes = Math.floor((sec_num - (hours * 3600)) / 60); var seconds = sec_num - (hours * 3600) - (minutes * 60); if (hours < 10) {hours = "0"+hours;} if (minutes < 10) {minutes = "0"+minutes;} if (seconds < 10) {seconds = "0"+seconds;} return hours+':'+minutes+':'+seconds; } 

Use so

 document.getElementById("time").innerHTML = (t != 0) ? ("" + t).toHHMMSS() : ""; 
  • Thank you very much! Everything as I wanted. - Ilmir Murtazin
  • In this case, mark the answer as appropriate by clicking on the check mark) - Newbie