实现一个打点计时器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 1、从 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一个数字,每次数字增幅为 1
// 2、返回的对象中需要包含一个 cancel 方法,用于停止定时操作
// 3、第一个数需要立即输出
function count(start, end) {
console.log(start++);
var timer = setInterval(function() {
if (start <= end) {
console.log(start++);
} else {
clearInterval(timer);
}
}, 100);
return {
cancel: function() {
clearInterval(timer)
}
}
}
count(2, 10);
</script>
</body>
</html>
运行结果