Javascript 计时器仅用于分秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19429890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:33:57  来源:igfitidea点击:

Javascript timer just for minutes and seconds

javascripttimerjsfiddlestopwatch

提问by mike

I found a JSFiddlewith a timer that counts up every second. Except i want this to work with just the minutes and seconds. No hours.

我找到了 JSFiddle一个计时器,每秒计数一次。除了我希望它只在几分钟和几秒钟内工作。没有小时。

Any ideas?

有任何想法吗?

回答by Rayon

  • DATE_OBJ.getSeconds()to get seconds of Dateobject.
  • DATE_OBJ. getMinutes()to get minutes of Dateobject.
  • setIntervalto invoke handler function after every second(1000ms).
  • DATE_OBJ.getSeconds()获取Date对象的秒数。
  • DATE_OBJ. getMinutes()获取分钟的Date对象。
  • setInterval每秒调用一次处理函数(1000ms)。

var handler = function() {
  var date = new Date();
  var sec = date.getSeconds();
  var min = date.getMinutes();
  document.getElementById("time").textContent = (min < 10 ? "0" + min : min) + ":" + (sec < 10 ? "0" + sec : sec);
};
setInterval(handler, 1000);
handler();
<h1 id="time" style="text-align: center"></h1>

回答by Vandesh

Here's a very hackish approach - http://jsfiddle.net/gPrwW/1/

这是一个非常hackish的方法 - http://jsfiddle.net/gPrwW/1/

HTML-

HTML-

<div id="worked">31:14</div>

JS :

JS:

$(document).ready(function (e) {
    var $worked = $("#worked");

    function update() {
        var myTime = $worked.html();
        var ss = myTime.split(":");
        var dt = new Date();
        dt.setHours(0);
        dt.setMinutes(ss[0]);
        dt.setSeconds(ss[1]);

        var dt2 = new Date(dt.valueOf() + 1000);
        var temp = dt2.toTimeString().split(" ");
        var ts = temp[0].split(":");

        $worked.html(ts[1]+":"+ts[2]);
        setTimeout(update, 1000);
    }

    setTimeout(update, 1000);
});

回答by Tibos

The precise way of handling this is the following:

处理这种情况的确切方法如下:

  1. store the time of the start of the script
  2. in a function that gets called repeatedly get the time elapsed
  3. convert the elapsed time in whatever format you want and show it
  1. 存储脚本开始的时间
  2. 在被重复调用的函数中获取经过的时间
  3. 以您想要的任何格式转换经过的时间并显示它

Sample code:

示例代码:

var initialTime = Date.now();

function checkTime(){
  var timeDifference = Date.now() - initialTime;
  var formatted = convertTime(timeDifference);
  document.getElementById('time').innerHTML = '' + formatted;
}

function convertTime(miliseconds) {
  var totalSeconds = Math.floor(miliseconds/1000);
  var minutes = Math.floor(totalSeconds/60);
  var seconds = totalSeconds - minutes * 60;
  return minutes + ':' + seconds;
}
window.setInterval(checkTime, 100);

You can easily change the granularity of checking the time (currently set at 0.1 seconds). This timer has the advantage that it will never be out of sync when it updates.

您可以轻松更改检查时间的粒度(目前设置为 0.1 秒)。这个计时器的优点是它在更新时永远不会不同步。

回答by Emre

You can make a function that increments a counter every time it's called, shows the value as: counter/60 minutes, counter%60 seconds

您可以创建一个每次调用都会增加计数器的函数,将值显示为:counter/60 分钟,counter%60 秒

Then you can use the setIntervalfunction to make JavaScript call your code every second. It's not extremely precise, but it's good enough for simple timers.

然后您可以使用该setInterval函数让 JavaScript 每秒调用您的代码。它不是非常精确,但对于简单的计时器来说已经足够了。

回答by user8000103

var initialTime = Date.now();

function checkTime(){
var timeDifference = Date.now() - initialTime;
var formatted = convertTime(timeDifference);
document.getElementById('time').innerHTML = '' + formatted;
}

function convertTime(miliseconds) {
var totalSeconds = Math.floor(miliseconds/1000);
var minutes = Math.floor(totalSeconds/60);
var seconds = totalSeconds - minutes * 60;
return minutes + ':' + seconds;
}
window.setInterval(checkTime, 100);

回答by Hiny

This might be something? plain count up timer in javascript

这可能是什么? javascript中的简单计数计时器

It is based on the setInterval method

它基于 setInterval 方法

setInterval(setTime, 1000);