zoukankan      html  css  js  c++  java
  • 4.定时器

    定时器:id=setInterval(fun,30); 清除定时器:clearInterval(id);

    延时:id=setTimeOut(fun,30); 清除延时:clearTimeOut(id);


    str.toCharAt(i);获取字符串str第i个位置上的字符;兼容ie和高级浏览器;


    例子:简易时钟;

    function toDou(n)
    {
        if(n<10)
        {
            return '0'+n;
        }
        else
        {
            return ''+n;
        }
    }
    
    window.onload=function ()
    {
        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.charAt(i)+'.png';
            }
        }
        setInterval(tick, 1000);
        tick();
    };

    字符串连接:str=''+n;


    无缝滚动:

    window.onload=function ()
    {
        var oDiv=document.getElementById('div1');
        var oUl=oDiv.getElementsByTagName('ul')[0];
        var aLi=oUl.getElementsByTagName('li');
        
        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+2+'px';
        }
        var timer=setInterval(move, 30);
        
        oDiv.onmouseover=function ()
        {
            clearInterval(timer);
        };
        
        oDiv.onmouseout=function ()
        {
            timer=setInterval(move, 30);
        };
    };

    oUl.innerHTML+=oUl.innerHTML;


    延时提示框:

    window.onload=function ()
    {
        var oDiv1=document.getElementById('div1');
        var oDiv2=document.getElementById('div2');
        var timer=null;
        
        oDiv2.onmouseover=oDiv1.onmouseover=function ()
        {
            clearTimeout(timer);
            oDiv2.style.display='block';
        };
        oDiv2.onmouseout=oDiv1.onmouseout=function ()
        {
            timer=setTimeout(function (){
                oDiv2.style.display='none';
            }, 500);
        };
    };
  • 相关阅读:
    PHP页面跳转的几种方法
    PHP网站并发测试
    04-上传文件
    01-转>linux命令
    01-CDN的好处
    05-socket.io使用
    04-soket.io使用2 -数据同步简单聊天室效果
    03-socket.io 2.3.0版本的使用-用户请求接口,实时推送给前端数据
    02-转>
    跨域-转>预解析OPTIONS请求
  • 原文地址:https://www.cnblogs.com/maoduoduo/p/3140388.html
Copyright © 2011-2022 走看看