今天在某个前端交流群,有个小伙伴问了一个小功能,自己想了一下,代码如下,可以实现基本功能:
下面是html结构
1 <div id="demo"> 2 <input type="button" value="0" > 3 <input type="button" value="0" > 4 </div>
下面是js代码:
1 <script> 2 var ips = document.querySelectorAll('input'); 3 var timer; 4 for (var i = 0; i < ips.length; i++) { 5 ips[i].addEventListener('click', function(){ 6 for (var i = 0; i < ips.length; i++) { 7 clearInterval(ips[i].timer); 8 } 9 var that = this; 10 this.timer=setInterval(function(){ 11 that.value = that.value-0+1; 12 console.log(that.value) 13 },100); 14 }) 15 } 16 17 </script>