javascript-setInterval

定时器秒表

by boxcore

HTML

<div id="div1">
  <span id="pid">
			<script>
				var t=new Date();
				document.write(t.toLocaleString());
			</script>
		</span>
</div>


<script>

//秒表
var pobj = document.getElementById("pid");
var sobj = "";

start();

function sint() {
  var tt = new Date();
  var t = tt.toLocaleString();
  pobj.innerHTML = t;
}

function stop() {
  clearInterval(sobj);
}

function start() {
  stop();
  sobj = setInterval("sint()", 1000);
}

</script>
<button id="a" onclick="start()">继续</button>
<button id="b" onclick="stop()">停止</button>

CSS

#div1 {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 500px;
  height: 100px;
  background: #000;
  font-size: 30px;
  color: #0f0;
  line-height: 100px;
  margin-top: -50px;
  margin-left: -250px;
}

button {
  width: 100px;
  height: 50px;
  font-size: 30px;
}