setInterval()定时调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>定时器</title>
</head>
<body>
<h1 id="count">1</h1>
</body>
</html>
<script type="text/javaScript">
window.onload = function(){
var count = document.getElementById("count");
var num = 1;
var timer = setInterval(() => {
count.innerHTML = num++;
if (num == 11) {
clearInterval(timer);
}
}, 2000);
};
</script>
setTimeout()延时调用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>延时调用</title>
</head>
<body>
<h1 id="count"></h1>
</body>
</html>
<script type="text/javaScript">
window.onload = function(){
var timer = setTimeout(
function(){console.log(num++);}; 3000);
clearTimeout(timer);
}
</script>