首页 > js时间 怎么开始,也怎么停止?

js时间 怎么开始,也怎么停止?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<div id="time"></div>


 <button onclick="stops()">时光停留</button>
 <button onclick="start()">开始流走</button>
<script>
var stop=document.getElementById("stop");
var int=self.setInterval("clock()",50);
var stop=0;
function clock(){
  var t=new Date();
  var s=t.toLocaleTimeString();
  document.getElementById("time").innerHTML=s;
}

function start(){
   int=setTimeout("clock()",50);
}
function stops(){
   clearTimeout(int);
}
</script>
</body>
</html>

我的思路如何随意在停止按钮 不动。
然后在开始按钮 一动, 时间就可以走。
由于jsbin打不开。 请见谅。


试试看:jsfiddle

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


<button id="stopBtn">时光停留</button>
<button id="startBtn">开始流走</button>
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);
【热门文章】
【热门文章】