<button class="begain">开启定时器</button>
<button class="stop">停止定时器</button>
var begain = document.querySelector('.begain');
var stop = document.querySelector('.stop');
var timer = null; //全局变量 null是个空对象 ;设置var timer; 会返回undefined
var jishi = 0;
begain.addEventListener('click', function() {
timer = setInterval(function() {
jishi++;
console.log('开始-'+timer+'-'+jishi);
}, 1000);
})
stop.addEventListener('click', function() {
clearInterval(timer);
console.log('暂停-'+timer);
})
网友评论