<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js啟動停止setInterval - 何問起</title>
</head>
<body>
<script src="http://hovertree.com/ziyuan/jquery/jquery-1.12.1.min.js"></script>
<div id="hovertreetime"></div>
<div>
<a href="javascript:;" id="cOkHoverTree">停止</a>
</div>
<script type="text/javascript">
function GetBack() {
document.getElementById('hovertreetime').innerHTML = new Date().toLocaleTimeString();
}
var iCount = setInterval(GetBack, 1000);//間隔1秒執行
$("#cOkHoverTree").on("click", function () {
//alert("何問起,停止間隔執行");
clearInterval(iCount);
});
</script>
</body>
</html>
效果:
停止