第3章 操作BOM(了解即可)2020-04-17

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>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。