用setTimeout()方法来替换setInterval()
遇到需求是倒计时,首先想到的是用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
欢迎关注小程序,感谢您的支持!