定时器的使用01

关键词:定时器、清除定时器、Date对象、charAt()

定时器:间隔性(setInterval)、延时性(setTimeout);
清除定时器:clearInterval(name),name为需要关闭的定时器的名称;对应的还有clearTimeout;
Date(): 包括getHours(),getMinutes(),getSeconds(),getFullYear(),getMonth(),getDate(),getDay(),其中getMonth()获取的月份是从0开始的;
charAt: 兼容低版本的获取元素的方法,比如获取str中第i位元素:str[i]=str.charAt(i);

测试题

延时提示框(如何实现)

这个是延长提示的实际应用.png

代码演示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style lang="">
        .box{
            width: 350px;
            height: 200px;
        }
        #left{
            width: 200px;
            height: 200px;
            background-color: yellow;
            float: left;
            display: none;
        }
        #right{
            width: 100px;
            height: 100px;
            background-color: red;
            float: right;
        }
        
    </style>
</head>
<body>
    <div class="box">
        <div id="left"></div>
        <div id="right"></div>
    </div>
    <script>
        
        var oLeft = document.getElementById('left');
        var oRight = document.getElementById('right');
        var timer = null;

        // oRight.onmouseover = function(){
        //     clearTimeout(timer); // 防止鼠标离开左边 div 的时候 div 消失
        //     oLeft.style.display = 'block'
        // };
        // oRight.onmouseout = function(){
        //     timer = setTimeout(function(){
        //         oLeft.style.display = 'none'
        //     },500)
        // };
        // oLeft.onmouseover = function(){
        //     clearTimeout(timer)
        // };
        // oLeft.onmouseout = function(){
        //     timer = setTimeout(function(){
        //        oLeft.style.display = 'none'
        //     },500)
            
        //简化代码 合并 mouseover   和 mouseout
        oRight.onmouseover = oLeft.onmouseover = function () {
            clearTimeout(timer); // 防止鼠标离开左边 div 的时候 div 消失
            oLeft.style.display = 'block'
        };
        oRight.onmouseout = oLeft.onmouseout = function () {
            timer = setTimeout(function () {
                oLeft.style.display = 'none'
            }, 500)
        };
     
    </script>
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 4,654评论 0 1
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,073评论 19 139
  • 基础复习笔记 JS效果三要素: 时间 事件 运动轨迹(分步骤来实现先死后活法逐步来) (1)获取元素 : getE...
    2e9a10d418ab阅读 3,173评论 0 3
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 33,093评论 18 399
  • 某个早晨,我睁眼 看到自己的第一缕白发, 不相信时光飞逝; 在无人注意的黄昏, 悄悄走进理发店将白发焗黑, 那些有...
    小哧阅读 2,635评论 0 1

友情链接更多精彩内容