js学到现在才知道的var

变量必须用var声明,否则就是更新操作, 如果这个变量尚未声明过, 就会在全局自动声明并创建它

 贴上练习的定时器

 1 <div class="boc">
 2     <button>跳转</button>
 3     <button>停止</button>
 4 </div>
 5 
 6 <script>
 7     var a= document.getElementsByTagName("div");
 8     var bt1=a[0].firstElementChild;
 9     var bt2=bt1.nextElementSibling;
10     var p= document.createElement(p);
11     bt1.addEventListener('click',add);
12     function add() {
13         a[0].appendChild(p);
14         p.innerHTML="正在跳转";
15         tz=setTimeout(function () {
16             location.href="http://www.baidu.com";
17         },3000);
18         console.log(tz);
19     }
20     bt2.addEventListener("click",remove);
21 
22     function remove(){
23         clearTimeout(tz);
24         p.innerHTML="";
25         console.log(tz);
26     }
27 </script>

猜你喜欢

转载自www.cnblogs.com/no000000/p/12410704.html