当前位置:网站首页>Setinterval, setTimeout and requestanimationframe
Setinterval, setTimeout and requestanimationframe
2022-06-29 10:04:00 【Zhan sir (open source byte)】
setTimeoutsetTimeout Delay execution of a piece of code , but setTimeout because EventLoop The existence of , Not 100% on time , One setTimeout It may be expressed in the following form :
// Time delay 1s after , Print hello,world
setTimeout(() => {
console.log('hello,world');
}, 1000)setInterval:setInterval Within a specified time , Repeat a piece of code , And setTimeout similar , It's not on time , And sometimes it is not recommended setInterval Timer , Because it works with some time-consuming code , There will be problems of execution accumulation , It will wait until the time-consuming operation is over , One or more execution timers together , There are performance issues . One setInterval It may be expressed in the following form :
setInterval(() => {
console.log('hello,world');
}, 1000)requestAnimationFrame: Request animation frame , It is html5 Designed specifically for requesting animation API, It is associated with setTimeout Compared with the following advantages :
- According to the refresh rate of different screens , Automatically adjust the timing of executing the callback function .
- When the window is inactive ,
requestAnimationFrameWill stop executing , andsetTimeoutCan't - It has function throttling function
var progress = 0;
var timer = null;
function render() {
progress += 1;
if (progress <= 100) {
console.log(progress);
timer = window.requestAnimationFrame(render);
} else {
cancelAnimationFrame(timer);
}
}
// First frame rendering
window.requestAnimationFrame(render);If you reprint , Please indicate the source : Open source byte https://sourcebyte.cn/article/171.html
边栏推荐
- Constructing SQL statements by sprintf() function in C language
- Middle order traversal of Li Kou 94 binary tree
- 通用分页框架
- Gmail:如何快速将邮件全部已读
- Container of the basic component of the flutter
- leetcode MYSQL数据库题目180
- Monitoring data source connection pool usage
- 2019icpc上海区域赛赛后总结
- 券商经理给的开户二维码办理股票开户安全吗?我想开个户
- Cloud management platform: openstack architecture design and detailed interpretation
猜你喜欢

container

Image of the basic component of the shutter

How to traverse objects in the vector container

A method of creating easy to manage and maintain thread by C language

RecyclerView 通用适配器封装

Student增删gaih

力扣94二叉树的中序遍历

Constructing SQL statements by sprintf() function in C language

另类实现 ScrollView 下拉头部放大

Listview of the basic component of the shutter
随机推荐
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
Idea auto completion
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
JVM之对象的内存布局
Leetcode MySQL database topic 177
RecyclerView 通用适配器封装
FreeRTOS(九)——队列
Introduction to intranet penetration tool FRP
Slider validation code
动态规划总结
Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks
完美二叉树、完全二叉树、完满二叉树
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
C # judge whether the array contains any items of another array
監控數據源連接池使用情况
微信小程序重写Page函数,实现全局日志记录
Cisco ASA、FTD和HyperFlex HX的漏洞分析复现
367. effective complete square dichotomy
Student增删gaih