网站首页 > 知识剖析 正文
setTimeout setTimeout延时执行某一段代码,但setTimeout由于EventLoop的存在,并不百分百是准时的,一个setTimeout可能会表示如下的形式:
// 延时1s之后,打印hello,world
setTimeout(() => {
console.log('hello,world');
}, 1000)
setInterval: setInterval在指定的时间内,重复执行一段代码,与setTimeout类似,它也不是准时的,并且有时候及其不推荐使用setInterval定时器,因为它与某些耗时的代码配合使用的话,会存在执行积累的问题,它会等耗时操作结束后,一起一个或者多个执行定时器,存在性能问题。一个setInterval可能会表示如下的形式:
setInterval(() => {
console.log('hello,world');
}, 1000)
requestAnimationFrame: 翻译过来就是请求动画帧,它是html5专门用来设计请求动画的API,它与setTimeout相比有如下优势:
- 根据不同屏幕的刷新频率,自动调整执行回调函数的时机。
- 当窗口处于未激活状态时,requestAnimationFrame会停止执行,而setTimeout不会
- 自带函数节流功能
var progress = 0;
var timer = null;
function render() {
progress += 1;
if (progress <= 100) {
console.log(progress);
timer = window.requestAnimationFrame(render);
} else {
cancelAnimationFrame(timer);
}
}
//第一帧渲染
window.requestAnimationFrame(render);
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/171.html
猜你喜欢
- 2024-11-11 windows时间同步服务器设置 win10时间同步服务器设置
- 2024-11-11 javaScript 使用定时器 js实现定时器功能
- 2024-11-11 关于JavaScript中this关键字指向的问题
- 2024-11-11 如何理解JavaScript定时器的4种写法「带面试题讲解」
- 2024-11-11 如何设置Windows系统 Internet时间的同步周期
- 2024-11-11 JavaScript 定时器和延时器 js定时器缺点
- 2024-11-11 手把手教你制作智能桌宠(小可爱哦!)
- 2024-11-11 setInterval()和setTimeout()区别及清除定时器
- 2024-11-11 第37节 计时器setTimeout和setInterval
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)