3-3 setTimeout 模拟 setInterval

var timer;
var i = 1;
timer = function () {
i++;
console.log(i);
if (i === 10) {
timer = function () {
console.log("终止运行");
}
}
setTimeout(timer, 3000);
};
setTimeout(timer, 3000);

解释:利用 setTimeout 延迟 执行 timer 函数, 函数内部 判读 i === 10 后,将 timer 的函数替换,函数将不会有 setTimeout 了,自然就停止

本文作者:林见夕

本文链接:https://www.cnblogs.com/ifon/p/15962270.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   林见夕  阅读(121)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.