简体   繁体   English

Javascript计时器只需几分钟和几秒钟

[英]Javascript timer just for minutes and seconds

I found a JSFiddle with a timer that counts up every second. 我找到了一个带有计时器的JSFiddle ,它每秒都会计数。 Except i want this to work with just the minutes and seconds. 除了我希望这只与分钟和秒钟一起工作。 No hours. 没有时间。

Any ideas? 有任何想法吗?

  • DATE_OBJ.getSeconds() to get seconds of Date object. DATE_OBJ.getSeconds()获取Date对象的秒数。
  • DATE_OBJ. getMinutes() DATE_OBJ. getMinutes() to get minutes of Date object. DATE_OBJ. getMinutes()获取Date对象的分钟数。
  • setInterval to invoke handler function after every second( 1000ms ). 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> 

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);
});

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 以您想要的任何格式转换已用时间并显示它

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). 您可以轻松更改检查时间的粒度(当前设置为0.1秒)。 This timer has the advantage that it will never be out of sync when it updates. 该计时器的优点是更新时永远不会失去同步。

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 setInterval function to make JavaScript call your code every second. 然后你可以使用setInterval函数让JavaScript每秒调用你的代码。 It's not extremely precise, but it's good enough for simple timers. 它不是非常精确,但它对于简单的计时器来说已经足够了。

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);

This might be something? 这可能是什么? plain count up timer in javascript 在javascript中普通计数计时器

It is based on the setInterval method 它基于setInterval方法

setInterval(setTime, 1000);

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

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