JavaScript定时器

定时器的开启与关闭

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<script>
var timer=null;
function startMove(){
clearInterval(timer);
var oDiv=document.getElementById('div1');
timer=setInterval(function(){
if(oDiv.offsetLeft==0)
{
clearInterval(timer);
}
else {
oDiv.style.left=oDiv.offsetLeft+10+'px';
}
},30)
}
function startMove1(){
clearInterval(timer);
var oDiv=document.getElementById('div1');
timer=setInterval(function(){
if(oDiv.offsetLeft==-200)
{
clearInterval(timer);
}
else {
oDiv.style.left=oDiv.offsetLeft-10+'px';
}
},30)
}
window.onload=function(){
var oDiv=document.getElementById('div1');
oDiv.onmouseover=function(){
startMove();
}
oDiv.onmouseout=function(){
starMove1();
}
</script>
Fork me on GitHub