简体   繁体   English

JS 如何像 Timer 一样减去时间(包括毫秒)

[英]JS How to subtract time (incl. milliseconds) like a Timer

I'm new here and I'm trying to write some code for myself.我是新来的,我正在尝试为自己编写一些代码。

I do have my own "clock" in this code.我在这段代码中有自己的“时钟”。 Then I specify an exact time.然后我指定一个确切的时间。

Then I want to subtract from the exact time I specified before the actual time (like a timer or countdown) with ms.然后我想用毫秒减去我在实际时间(如计时器或倒计时)之前指定的确切时间。

But I really don't know how.但我真的不知道怎么做。

This is my code so far:到目前为止,这是我的代码:

<!DOCTYPE html>
<html>
<head>
<title>DSTimer</title>
</head>
<body>
<center><font face="Verdana" size="40">DS-Timer</font><br /></center>
<br />
<br />
<font size="+7" </font>
<p id="DS-Timer">getElementById</p>
<font size="-1" </font>
<p id="ABC"></p>
<font size="+1" </font>
    <label class="h2" for="delay">Verbindungszeit zum Server</label>
    <input type="number" name="Verbindungszeit zum Server" id="delay" maxlength="3">
    <button onclick="addDelay()" type="submit">OK</button>    

<p> <label for="Abschickzeitpunkt">Abschickzeitpunkt</label>
    <input type="time" step="0.001" id="ABZ">
    <button onclick="insert()" type="submit">OK</button></p>        

    <button type="reset">Eingaben zurücksetzen</button>

</form>
</body>
<script>
var delay = 0;
timer();
function timer() {
    var now = new Date();

    var test = new Date(now.getTime() + delay),
        h = test.getHours(),
        m = test.getMinutes(),
        s = test.getSeconds(),
        ms = test.getMilliseconds();

    m = zeroFill(m);
    s = zeroFill(s);
    ms = zeroFill(ms);
    document.getElementById('DS-Timer').innerHTML = h + ':' + m + ':' + s + ':' + ms;
    setTimeout(timer, 5);
}
function zeroFill(filler) {
    filler = (filler < 10 ? '0' : '') + filler;
    return filler;
}
function addDelay() {
    var d = document.getElementById('delay').value;
    delay = parseFloat(d);
}
function insert() {
    var x = document.getElementById("ABZ").value
    document.getElementById("ABC").innerHTML = x;  
}    
</script>
</html>```

So I want to calculate "DS-Timer - ABC".

You can use the Performance interface in most modern Javascript engines.您可以在大多数现代 Javascript 引擎中使用性能接口。

 let start = performance.now(); let timer = document.getElementById("timer"); for(let i = 0;i<5000;i++){ timer.innerText = i; } let end = performance.now(); let elapsed = end - start; timer.innerText += " that took "+elapsed+" ms"
 <h1 id="timer"></h1>

Read more about it here: Performance Interface在此处阅读有关它的更多信息:性能接口

This is how it worked.这就是它的工作原理。 :-) :-)

var z = (document.getElementById("ABZ").value).split(':').join(',').split('.').join(',').split(',').join(',').split(',');
var y = new Date();
var generatedDate = new Date(y.getFullYear(), y.getMonth(),y.getDate(),z[0], z[1], z[2], z[3], z[4], z[5]);
var calculatedDate = generatedDate - y;
var h = Math.floor((calculatedDate % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var m = Math.floor((calculatedDate % (1000 * 60 * 60)) / (1000 * 60));
var s = Math.floor((calculatedDate % (1000 * 60)) / 1000);
var ms = Math.floor((calculatedDate % 1000));
document.getElementById("COUNTDOWN").innerHTML = ( h + ':' + m + ':' + s + ':' + ms);```

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

相关问题 打印带有textarea incl的div。 使用js的用户输入 - print div with textarea incl. user input using js React JS:通过另一个对象数组过滤一个对象数组。 如何顺序执行四个功能,包括 几个API调用 - React JS: Filtering an array of objects by another array of objects. How to sequential execute four functions incl. several API calls 如何将选项链接到订单,包括。 期权的数量? - How to link options to orders incl. the quantity of options? 如何根据此方法在 Google Analytics 中跟踪出站链接,包括标签和非交互? - How to track outbound links in Google Analytics according to this method, incl. Label & Non-Interaction? 将 Javascript Object(包括函数)转换为字符串 - Convert Javascript Object (incl. functions) to String 如何使用 javascript 中的按钮从计时器中添加和减去时间 - how to add and subtract time from a timer with a button in javascript JS中以毫秒为单位的时间计算 - Time calculation with milliseconds in JS JS-在MySQL中添加计时器作为日期时间(以毫秒为单位) - JS - add timer in mysql as datetime with milliseconds 在注释之间移动HTML代码(包括注释) - Move HTML Code between comment (incl. comments) jQuery的替换:$(&#39;#id&#39;)。html(HTMLfragment)//含。 <script> execution - Replacement for jQuery's: $('#id').html(HTMLfragment) // incl. <script> execution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM