setTimeout函数
让函数在一定时间内重新执行,递归调用
如果不递归调用则仅执行一次
clearTimeout函数
清除设置的setTimeout函数
clearTimeout(timeId);
setInterval函数
让函数在一定时间内重新执行,外部调用
clearInterval函数
清除设置的setInterval函数
clearInterval(timeId);
//重点 前端考试
function aaa(){
alert("你好")
}
//setTimeout(aaa(), 5000); //延迟5秒后弹出
//aaa();
//间隔函数
setInterval("aaa()",5000);
例setInterval 清除设置的setTimeout函数
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script language=Javascript>
var timeId;
function hello(){
alert("hello");
}
function do_set(){
timeId = setInterval("hello()",2000);
}
function cInterval(){
clearInterval(timeId);
}
</script>
</head>
<body>
<input type="button" value="setInterval()" onclick="do_set()">
<input type="button" value="clearInterval()" onclick="cInterval()">
</body>
</html>
例setTimeout 清除设置的setTimeout函数
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script language=Javascript>
var timeId;
function hello(){
alert("hello");
timeId = setTimeout("hello()",2000);
}
function cTimeout(){
clearTimeout(timeId);
}
</script>
</head>
<body>
<input type="button" value="setTimeout()" onclick="hello()">
<input type="button" value="clearTimeout()" onclick="cTimeout()">
</body>
</html>