用setTimeout()方法来替换setInterval()

作者: MJ 分类: javascript 发布时间: 2019-05-17 14:13

遇到需求是倒计时,首先想到的是用setInterval来实现,但是如果切换浏览器倒计时就会越来越快,为了避免这种问题就用setTimeout来模拟setInterval。

"use strict";
setTimeout(function () {
    // 任务
    setTimeout(arguments.callee, interval);
}, interval)

但是在实际应用的时候会遇到,如果js在严格模式 “use strict”下会出现报错:

Uncaught TypeError: ‘caller’, ‘callee’, and ‘arguments’ properties may not be accessed on strict mode functions or the arguments objects for calls to them at

解决方案就是:

"use strict";
setTimeout(function f() {
    // 任务
    setTimeout(f, interval);
}, interval)

参考:

https://blog.csdn.net/baidu_24024601/article/details/51862488

https://segmentfault.com/a/1190000011995367

欢迎关注小程序,感谢您的支持!

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

邮箱地址不会被公开。 必填项已用*标注