当前位置:网站首页>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
边栏推荐
- Hystrix熔断器:服务熔断与服务降级
- 数据可视化:数据可视化的意义
- C语言中通过sprintf()函数构造sql语句
- 请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
- A 2.5D Cancer Segmentation for MRI Images Based on U-Net
- FreeRTOS(八)——时间管理
- Data visualization: the four quadrants of data visualization teach you to correctly apply icons
- MySQL modify auto increment initial value
- 微信小程序实现store功能
- Set up lamp environment under cenos7
猜你喜欢

IPC(进程间通信)之管道详解

Making of simple addition calculator based on pyqt5 and QT Designer

How to set Google Chrome as the default browser

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

Student增删gaih

基于stm32标准库独立按键的多按键状态机的实现

Visual assist plug-in settings for UE4 vs

User level threads and kernel level threads

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h
随机推荐
Hystrix熔断器:服务熔断与服务降级
JS obtain mobile phone model and system version
股票炒股账号开户安全吗?是靠谱的吗?
在Activity外使用startActivity()方法报错原因与解决办法
LeetCode刷题——泰波那契数列
Generic paging framework
通用分页框架
Cloud management platform: 9 open source cloud management platforms (CMP)
How to set Google Chrome as the default browser
es报错NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
kdevelop新建工程
leetcode MYSQL数据库题目180
JS获取手机型号和系统版本
After installing anaconda, you need to enter a password to start jupyterlab
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
阿里云服务器安装配置redis,无法远程访问
UE4 blueprint modify get a copy in array to reference
PHP内存马技术研究与查杀方法总结
【NOI模拟赛】为NOI加点料(重链剖分,线段树)
完美二叉树、完全二叉树、完满二叉树