1 Star 0 Fork 9

caoyongxi/react_hooks_fiber

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
idle.html 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Idle示例</title>
</head>
<body>
<script>
function sleep(delay) {
for (let now = Date.now(); Date.now() - now < delay; ) {}
}
let allStart = 0;
const works = [
() => {
allStart = Date.now();
console.log("this first work start");
sleep(20);
console.log("this first work end");
},
() => {
console.log("this second work start");
sleep(15);
console.log("this second work end");
},
() => {
console.log("this third work start");
sleep(10);
console.log("this third work end");
console.log(Date.now() - allStart);
},
];
requestIdleCallback(workLoop, { timeout: 1000 });
// deadline参数是一个对象,有2个属性
// timeRemaining(), 返回此帧还剩下多少时间让用户使用
// didTimeout 判断回调任务callback是否超时
function workLoop(deadline) {
console.log(`this frame remainTime is ${deadline.timeRemaining()}`);
while (
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
works.length > 0
) {
performUnitOfWork();
}
if (works.length > 0) {
window.requestIdleCallback(workLoop, { timeout: 1000 });
}
}
function performUnitOfWork() {
works.shift()();
}
</script>
</body>
</html>
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/caoyongxi_gitee/react_hooks_fiber.git
[email protected]:caoyongxi_gitee/react_hooks_fiber.git
caoyongxi_gitee
react_hooks_fiber
react_hooks_fiber
master

搜索帮助