Blogger Information
Blog 67
fans 0
comment 2
visits 71985
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js事件,线程,定时器
搁浅
Original
545 people have browsed it
js单线程:同一时刻只能执行一段代码,按书写顺序执行。
定时器:代码会放到任务队列执行,始终会在单线程后执行。
事件循环:就是不断的把定时器等事件放回到主线程执行。
它们解决了事件堵塞的问题,不需要把所有事件都同步执行。
  • html代码
    1. <button onclick="alert('哈哈')">按钮1</button>
    2. <button>按钮2</button>
    3. <button>按钮3</button>
    4. <button onclick="se(event)">按钮4</button>
  • 选择按钮
    1. const btn=document.querySelector("button:nth-of-type(2)");
    2. btn.addEventListener("click",()=>console.log(1)); //测试按钮
  • 事件删除
    1. let showtime=()=>console.log('事件');
    2. btn.removeEventListener("click",btn);
  • 事件派发
    1. const btn3=document.querySelector("button:nth-of-type(3)");
    2. let j=0;
    3. btn3.addEventListener("click",()=>{
    4. console.log("加钱:"+j+"元");
    5. j+=1;
    6. });
    7. setTimeout(()=>console.log(2),2000);//一次性定时器函数
    8. setInterval(()=>console.log(2),2000);//间歇性定时器
  • 事件冒泡
    子元素的同名事件,会沿着父级一级一级向上依次触发同名事件。
    1. function se(eve){
    2. console.log(eve.type);
    3. }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post