forked from WangJia-mm/JavaScript201708
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-4.js
More file actions
42 lines (27 loc) · 917 Bytes
/
1-4.js
File metadata and controls
42 lines (27 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//->需求:开始进来输出1,以后每隔1S钟都累加1,到5结束
// var n = 0;
// function fn() {
// n++;
// console.log(n);
// if (n >= 5) {
// clearInterval(timer);
// }
// }
// fn();
// var timer = setInterval(fn, 1000);
//------
//->使用递归实现当前的需求:setTimeout模拟出setInterval的效果
//->递归:函数执行的时候,在调用自己执行
var n = 0,
timer = null;
function fn() {
//->执行FN的时候,上一次创建的那个定时器已经没用了,为了节约内存和性能,我们最好把没用的这个定时器给清除掉
clearTimeout(timer);//->清除上一次设置的定时器
console.log(++n);
if (n >= 5) {
return;
}
//->arguments.callee:当前函数本身(JS严格模式下不允许使用,所以真正项目中我们基本上不用这个属性)
timer = setTimeout(fn, 1000);
}
fn();