html之事件传播机制和定时器

事件传播机制:当元素嵌套且绑定相同类型事件时,会自动触发其父辈绑定的事件

阻止事件传播

html 复制代码
//阻止事件传播
var box = document.querySelector(".box")  
box.onclick=function(e){}
e.stopPropagation()

监听事件

html 复制代码
//添加事件监听器
//box.addEventListener(事件类型,事件函数,事件传播阶段) 
var child = document.querySelector(".child")  //孩子
var parent = document.querySelector(".parent") //父亲
var grandpa = document.querySelector(".grandpa")  //爷爷
child.addEventListener("onclick",function(){},false)     //默认false,为冒泡阶段;true,为捕获阶段
//加上之后则执行到child就不会自动执行父亲和爷爷的点击事件

周期定时器和延迟定时器

setInterval(function,time):周期定时器 function指执行函数,time指时间间隔,单位是毫秒

clearInterval(定时器标志变量) :定时器停止

setTimeout(function,time):延迟定时器 function指执行函数,time指时间间隔,单位是毫秒

clearTimeout(定时器标志变量) :定时器停止

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #aa{
            width: 130px;
            height: 130px;
            background: linear-gradient(0deg, red, yellow, blue);
        }
    </style>
</head>
<body>
    <div id="aa"></div>
    <!-- <input type="range" id="change" min="0" value="50" max="360"> -->
    <input type="button" value="停止" id="stop">
    <input type="button" value="开启" id="start">
    <script>
        // setInterval(function,time):周期定时器   function指执行函数,time指时间间隔,单位是毫秒
        // setTimeout(function,time):延迟定时器   function指执行函数,time指时间间隔,单位是毫秒
        // var c_btn = document.getElementById("change")
        var c_div = document.getElementById("aa")
        var stop = document.getElementById("stop")
        var start = document.getElementById("start")
        // c_btn.oninput=function(){
        //     var current_value = this.value
        //     c_div.style.background="linear-gradient("+current_value+"deg, pink, yellow, blue)"
        // }
        
        var current_value = 0
        var once = 1
        var demo = function(){
            c_div.style.background="linear-gradient("+(current_value++)+"deg, red, yellow, blue)"
            test = setTimeout(demo,100)
        }
        
        // var test = setInterval(demo,100)
        var test = setTimeout(demo,100)
        stop.onclick=function(){
            // clearInterval(test)
            clearTimeout(test)
            once = 0
        }
        start.onclick=function(){
            if(once==1){
                return
            }
            // test = setInterval(demo,100)
            test = setTimeout(demo,100)
            once=1
        }
    </script>
</body>
</html>
相关推荐
Patrick_Wilson2 小时前
router.replace 之后紧跟 reload,页面为什么无限刷新?
javascript·react.js·浏览器
秋93 小时前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考
mONESY3 小时前
JavaScript 栈、队列、数组与链表核心知识点总结
javascript·面试
huangdong_3 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
ZengLiangYi3 小时前
TypeScript 项目配置:tsconfig、ESM、路径别名
javascript·typescript·aigc
chase_my_dream4 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
晓13134 小时前
【Cocos Creator 3.x】篇——第二章 入门
前端·javascript·游戏引擎
想要成为糕糕手4 小时前
前端必修课:JavaScript 数组与数据结构底层逻辑全解析
javascript·数据结构·面试
Cloud_Shy6184 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
xiaofeichaichai4 小时前
React Hooks
前端·javascript·react.js