timer

native timer

by leftstick

HTML

<div id="time"></div>


<button id="stopBtn">时光停留</button>
<button id="startBtn">开始流走</button>

JavaScript

var stop=document.getElementById("stop");
var intervalId;

function clock(){
  var t=new Date();
  var s=t.toLocaleTimeString();
  document.getElementById("time").innerHTML = s;
}

document
    .querySelector('#startBtn')
    .addEventListener('click', function(){
        intervalId = setInterval(clock, 50);
        this.disabled = true;
        document.querySelector('#stopBtn').disabled = false;
    }, false);
    
document
    .querySelector('#stopBtn')
    .addEventListener('click', function(){
        clearTimeout(intervalId);
        this.disabled = true;
        document.querySelector('#startBtn').disabled = false;
    }, false);