setTimeout 延时定时器的使用小案例

来源:互联网 发布:化学博士就业前景 知乎 编辑:程序博客网 时间:2024/05/22 09:48
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width:50px;
height:50px;
background-color:red;
margin:5px;
float: left;
}
#div2{
width:200px;
height: 200px;
background-color:greenyellow;
display: none;
}
</style>
<script type="text/javascript">
window.onload=function(){
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
var timer=null;

oDiv1.onmouseover=oDiv2.onmouseover=function(){
clearTimeout(timer);
oDiv2.style.display='block';
}

oDiv1.onmouseout=oDiv2.onmouseout=function(){
timer=setTimeout(function(){
oDiv2.style.display='none';
},1000);
}
}
</script>
</head>
<body>
<div id="div1"></div>
<div id="div2"></div>
</body>
</html>
0 0
原创粉丝点击