Javascript秒到分钟:秒
Javascript Second into Minutes:Seconds
本文关键字:分钟 Javascript 更新时间:2023-09-26
我正在处理一个需要在中显示秒数的时钟
分钟:秒
格式。
我做过一些显示的辅助功能,但我从来没有真正得到完整的显示。这是我的一些代码:
var time = 1500;
//Must declare timeHandler as global variable for stopTimer()
var timeHandler;
//Set intial HTML to time
document.getElementById("timer").innerHTML = display;
//Timer function for start button
function timer() {
timeHandler = setInterval(function() {
if (time > 0) {
time--;
document.getElementById("timer").innerHTML = time;
}
}, 1000);
}
//Stop function for stop button
function stopTimer() {
clearTimeout(timeHandler);
}
//Timer Display
var minutes = time/60;
var second = time%60;
var display = minutes + ":" + seconds;
HTML:
<h1> Pomodoro Clock</h1>
<!--Place holder for timer-->
<div id="timer" class="circle">Timer</div>
<!--//Start Button-->
<button onclick="setTimeout(timer, 1000);">Start</button>
<!--Stop Button-->
<button onclick="stopTimer()">Stop</button>
下面的formatTime
函数需要几秒钟的时间,并将其转换为MM:SS格式(包括填充的零):
var time = 1500;
//Must declare timeHandler as global variable for stopTimer()
var timeHandler;
//Set intial HTML to time
document.getElementById("timer").innerHTML = display;
//Timer function for start button
function timer() {
timeHandler = setInterval(function() {
if (time > 0) {
time--;
document.getElementById("timer").innerHTML = formatTime(time);
}
}, 1000);
}
//Stop function for stop button
function stopTimer() {
clearTimeout(timeHandler);
}
function formatTime(seconds) {
//Timer Display
var minutes = seconds / 60;
var second = seconds % 60;
return ('0'+minutes).substr(-2) +":"+ ('0'+seconds).substr(-2);
}
首先你必须使用clearInterval
,然后你不会每秒钟更新一次你的display
检查这个小提琴
相关文章:
- Javascript倒计时计时器倒计时未正确更新秒/分钟
- 如何在javascript中将秒转换为分钟和小时
- 如何使用javascript将秒转换为分钟.
- 计算JavaScript中的时间差异(天+小时+分钟+秒)
- 使用 javascript 将指定的分钟数添加到时间
- 如何运行javascript并每分钟重新加载DIV容器(计时器)
- 用javascript将时间格式化为小时和分钟
- 如何设置此javascript以正确显示花费的总时间,显示超出正常时钟的分钟数
- 用javascript将pubDate转换为x分钟前
- 如何从javascript或jquery中的时间字符串中获取分钟数
- Javascript模糊时间(例如'10分钟前');s精确到几秒钟
- 用于单击页面上的按钮的Javascript,每隔几分钟就会加载一次
- 如何在javascript中获得两个原始日期之间的分钟差
- Javascript函数每分钟更改一次链接
- 在 Node.js 中向 javascript Date 添加分钟数
- Javascript时钟,落后几分钟
- 我想要一个警报框,在倒数计时器中使用 javascript 的 10 分钟
- 在 JavaScript 中,范围小时 30 分钟(或 20 分钟)
- 获取以分钟为单位的当前日期时间,并将日期时间(d.m.Y H:m)转换为分钟Javascript
- 获取从开始到结束所花费的分钟javascript NaN错误