当前位置:网站首页>setInterval、setTimeout和requestAnimationFrame
setInterval、setTimeout和requestAnimationFrame
2022-06-29 09:15:00 【詹Sir(开源字节)】
setTimeoutsetTimeout延时执行某一段代码,但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
边栏推荐
- F5 BIG-IP iControl REST命令执行(CVE-2022-1388)
- Closed training (25) basic web security
- Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
- Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
- Idea auto completion
- Surveiller l'utilisation du pool de connexion des sources de données
- 长安链GO语言智能合约环境搭建及使用
- 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
- 基于keil5自动配置stm32f103标准库的官网freertos移植
- A 2.5D Cancer Segmentation for MRI Images Based on U-Net
猜你喜欢

Student addition / deletion gaih

UE4 material UV texture does not stretch with model scale

UE4 compile a single file (VS and editor start respectively)

CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION

Closed training (25) basic web security

Chang'an chain go language smart contract writing and compilation

Mysql配置主从数据库
![[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)](/img/d4/f5ea847573433f7ca7bd429f57e40a.png)
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)

基於PyQt5和Qt Designer的簡易加法計算器的制作

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)
随机推荐
leetcode MYSQL数据库题目177
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
gcc与makefile
Basic operations of MAC MySQL database
Lc236. nearest common ancestor of binary tree
转载 :判断对象是否具有属性的5种方法
动态规划总结
linux环境下安装配置redis,并设置开机自启动
数据治理:元数据管理(第二篇)
容器
leetcode MYSQL数据库题目180
UE4 compile a single file (VS and editor start respectively)
分布式和集群分不清,我们讲讲两个厨子炒菜的故事
watch监听和computed计算属性的使用和区别
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
JS obtain mobile phone model and system version
语言特性
[noi Simulation Competition] add points for noi (heavy chain dissection, line segment tree)
Please use the learned knowledge to write a program to find out the password hidden in the long string below. The burial point of the password conforms to the following rules:
Monitoring data source connection pool usage