JavaScript定时器取消后怎样重新启动??

论坛 期权论坛 留学     
剑人曾增   2019-6-27 21:03   6636   4
我这个定时器关闭后怎样重新启动不会吧所有的函数再在里面写一遍吧?<!doctypehtml><htmllang="en"><head><metacharset="utf-8"><title>document</title><style>#div1img{height:200p... 我这个定时器关闭后怎样重新启动 不会吧所有的函数再在里面写一遍吧?
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>document</title>
<style>
#div1 img{ height:200px; width:200px;position:absolute;}
#div1 .active{ background:yellow;}
img{ height:100%;width:100%;}
</style>
<script>
window.onload=function(){

var oDiv1=document.getElementById('div1');

var aBtn=oDiv1.getElementsByTagName('input');
var aDiv=oDiv1.getElementsByTagName('div');
var aImg=oDiv1.getElementsByTagName('img');
var n=0;
var t=0;
t=setInterval(function(){
n++;
n=n%aBtn.length;
for(var i=0;i<aBtn.length;i++){
aBtn[i].className="";
aImg[i].style.display="none";
}
aImg[n].style.display="block";
aBtn[n].className="active";
aImg[n].onmouseover=function(){
clearInterval(t)
};
aImg[n].onmouseout=function(){
//这里怎么写?
};
},500);

}
</script>
</head>
<body>
<div id="div1">
<input class="active" type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="4">
<div><img style="display:block" src="1.jpg"></div>
<div><img src="2.jpg"></div>

<div><img src="3.jpg"></div>
<div><img src="4.jpg"></div>
</div>
</body>
</html>
分享到 :
0 人收藏

4 个回复

倒序浏览
2#
剑人曾增  1级新秀 | 2019-6-27 21:03:15
答主,fun1函数不用var声明的话会报错哦
应该能是这样吧--
var t=setInterval(fun1,500)//fun1是你的函数
var fun1=function(){
//写入你的函数
alert("执行代码");
}
clearInterval(t)//清除定时器
t=setInterval(fun1,500)//重新开始定时器
3#
tatooo  1级新秀 | 2019-6-27 21:03:16

定时器里的函数要单独定义一下,不要直接写在定时器里

如下

var t=setInterval(fun1,500)//fun1是你的函数
fun1=function(){
//写入你的函数
}
clearInterval(t)//清除定时器
t=setInterval(fun1,500)//重新开始定时器
4#
xi8km9  1级新秀 | 2019-6-27 21:03:17
首先给函数个名字
t=setInterval(function self(){
然後

aImg[n].onmouseout=function(){
t=setInterval(self,500)
};

当然你也可以使用arguments.callee, 或者以下写法
function action()
{
...
t=setInterval(action,500)
...
}
t=setInterval(action,500)
总而言之都是要获取到函数本身
5#
MITKING12138  1级新秀 | 2019-6-27 21:03:18
var timer =function(){
timer1 = setInterval(move,5);}
timer();
tan.addEventListener("mouseover",function(){
clearInterval(timer1);
},false);
tan.addEventListener("mouseout",timer,false);
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7
帖子:2
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP