定时器的使用

定时器的作用

开启定时器
function show(){
            alert('a');
        }
        //setInterval(show,1000); 无限的执行
        setTimeout(show,1000);   //执行一次
停止定时器

setTimeout—clearTimeout
setInterval----clearInterval

 window.onload=function(){
            var oBtn1=document.getElementById('btn1');
            var oBtn2=document.getElementById('btn2');
            var timer=null;

            oBtn1.onclick=function(){
                timer=setInterval(function(){
                    alert('a');
                },3000);
            };
            oBtn2.onclick=function(){
                clearInterval(timer);
            
            };
        };
          <input id="btn1" type="button" value="开启">
    <input id="btn2" type="button" value="关闭">

数码时钟

获取系统时间
Data对象

var oDate=new Date();
        alert(oDate.getHours());  //小时
        alert(oDate.getMinutes()); //分
        alert(oDate.getSeconds()); //秒
<script>
        function toDou(n){
            if(n<10){
                return '0'+n;
            }
            else{
                return ''+n;
            }
        }

        window.onload=function(){
            // 显示差一秒的
            // var aImg=document.getElementsByTagName('img');
            // setInterval(function(){
            //     var oDate=new Date();

            //     var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
            // //alert(str)
            //     for(var i=0;i<aImg.length;i++){
            //         aImg[i].src='img/'+str[i]+'.png';
            //     }
            // },1000);

            //需要刷新的
            //var aImg=document.getElementsByTagName('img');
            // var oDate=new Date();
             // var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
            // //alert(str)
            // for(var i=0;i<aImg.length;i++){
            //     aImg[i].src='img/'+str[i]+'.png';
            //}

            //可以立刻显示
            var aImg=document.getElementsByTagName('img');
            function tick(){
                var oDate=new Date();

               var str=toDou(oDate.getHours())+toDou(oDate.getMinutes())+toDou(oDate.getSeconds());
            
                 for(var i=0;i<aImg.length;i++){
                     aImg[i].src='img/'+str[i]+'.png';
                 }
            }
            setInterval(tick,1000);
            tick();
        };
    </script>
<body style="background: black;color: white;font-size: 50px;">
    <img src="img/0.png" alt="">
    <img src="img/0.png" alt="">
    :
    <img src="img/0.png" alt="">
    <img src="img/0.png" alt="">
    :
    <img src="img/0.png" alt="">
    <img src="img/0.png" alt="">
</body>

这一段代码存在兼容问题;无法在IE7版本运行
原因:IE7 无法执行
alert(str[0])
修改方法:将alert(str[0])改为alert(str.charAt(0))

Data对象其他方法

alert(oDate.getFullYear()); //年
alert(oDate.getMonth()+1); //月 月是从0开始所以要加一
alert(oDate.getDate()); //日
alert(oDate.getDay()); //星期 0—周日 其他正常

延时提示框
<style>
        div{
            float: left;
            margin: 10px;
        } 
        #div1{
            width: 50px;
            height: 50px;
            background: red;
        }
        #div2{
            width: 250px;
            height: 180px;
            background: gray;
            display: none;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv1=document.getElementById('div1');
            var oDiv2=document.getElementById('div2');
            timer=null;
            oDiv1.onmouseover=function(){
                clearTimeout(timer);         //当鼠标回到div1清除计时器
                oDiv2.style.display='block'; 
            }
            oDiv1.onmouseout=function(){
                timer= setTimeout (function(){  //当鼠标移出div1使div2延时500毫秒消失
                    oDiv2.style.display='none';
                },500);
            };
            oDiv2.onmouseover=function(){
                clearTimeout(timer);       //当鼠标回到div2清除计时器
            }
            oDiv2.onmouseout=function(){    //当鼠标移出div2使div2延时500毫秒消失
                setTimeout (function(){
                    oDiv2.style.display='none';
                },500);
        }
        }
    </script>

<body>
    <div id="div1"></div>
    <div id="div2"></div>
</body>

## 代码简化

<script> //**onmouse简化代码**
 oDiv2.onmouseover=oDiv1.onmouseover=function(){
                clearTimeout(timer);         //当鼠标回到div1清除计时器
                oDiv2.style.display='block'; 
            }
            oDiv2.onmouseout=oDiv1.onmouseout=function(){
                timer= setTimeout (function(){  //当鼠标移出div1使div2延时500毫秒消失
                    oDiv2.style.display='none';
                },500);
            };
</script>

无缝滚动

offsetLeft/offsetTop/offsetWidth/offsetHeight

 <style>
        *{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 100px;
            height: 37px;margin: 100px auto;
            position: relative;
            background: red;
            overflow: hidden;
        }
        #div1 ul{
            position: absolute;
            left: 0;
            top: 0;
        }
        #div1 ul li {
            float: left;
            width: 25px;
            height: 37px;
            list-style: none;
        }
    </style>
    <script>
        window.onload=function(){
            var oDiv=document.getElementById('div1');
            var oUl=oDiv.getElementsByTagName('ul')[0];
            var aLi=oUl.getElementsByTagName('li');
            var speed=2;

            oUl.innerHTML=oUl.innerHTML+oUl.innerHTML;
            oUl.style.width=aLi[0].offsetWidth*aLi.length+'px';
            //简化代码
            function move(){
                if(oUl.offsetLeft<-oUl.offsetWidth/2){
                    oUl.style.left='0';
                }
                if(oUl.offsetLeft>0){
                    oUl.style.left=-oUl.offsetWidth/2+'px';
                }
                oUl.style.left=oUl.offsetLeft+speed+'px';
            }
            var timer= setInterval(move,30);
            oDiv.onmouseover=function(){
                clearInterval(timer);
            }
            oDiv.onmouseout=function(){
                timer= setInterval(move,30);
            }
            //向左走向右走
            document.getElementsByTagName('a')[0].onclick=function(){
                speed=-2;
            }
            document.getElementsByTagName('a')[1].onclick=function(){
                speed=2;
            }

             // 向左转动
            // setInterval(function(){
            //     if(oUl.offsetLeft<-oUl.offsetWidth/2){
            //         oUl.style.left='0';
            //     }
            //     oUl.style.left=oUl.offsetLeft-2+'px';
            // },30)

            // //向右转动
            // var timer= setInterval(function(){
            //     if(oUl.offsetLeft<-oUl.offsetWidth/2){
            //         oUl.style.left='0';
            //     }
            //     if(oUl.offsetLeft>0){
            //         oUl.style.left=-oUl.offsetWidth/2+'px';
            //     }
            //     oUl.style.left=oUl.offsetLeft+2+'px';
            // },30);

            // //移入暂停,移出开始
            // oDiv.οnmοuseοver=function(){
            //     clearInterval(timer);
            // }
            // oDiv.οnmοuseοut=function(){
            //     timer=setInterval(function(){
            //     if(oUl.offsetLeft<-oUl.offsetWidth/2){
            //         oUl.style.left='0';
            //     }
            //     if(oUl.offsetLeft>0){
            //         oUl.style.left=-oUl.offsetWidth/2+'px';
            //     }
            //     oUl.style.left=oUl.offsetLeft+2+'px';
            // },30);
            // }
        };

    </script>

<body>
    <!-- //οnclick="alert(this.offsetLeft)" -->
    <a href="javascript:;">向左走</a>
    <a href="javascript:;">向右走</a>
    <div id="div1" >
        <ul>
            <li><img src="img/1.png" alt=""></li>
            <li><img src="img/2.png" alt=""></li>
            <li><img src="img/3.png" alt=""></li>
            <li><img src="img/4.png" alt=""></li>
        </ul>

    </div>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值