Beispiel:JS-Date-now-1.html

Aus SELFHTML-Wiki
Wechseln zu: Navigation, Suche
<!doctype html>
<html lang="de">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./Beispiel:SELFHTML-Beispiel-Grundlayout.css">
  <title>Date.now() - 1</title>
<style>
p {
    border: medium solid;
    border-radius: 50%;
    width: 3em;
    margin-left: 2em;
    padding: 2em 1em;
    text-align: center;
}
</style>
<script>
'use strict';  
(function () {
  function timer() {
    const startTime = Date.now();

    let interval = setInterval(function() {
       let elapsedTime = Date.now() - startTime;
       document.getElementById('timer').textContent = (elapsedTime / 1000).toFixed(1);
    }, 100);
  }
 
  document.addEventListener('DOMContentLoaded',timer);
}());
</script>
</head>
 
<body>
<h1>Timer mit Date.now()</h1>

<p><output id="timer"></output> s</p>
</body>
</html>