window 对象
最后更新于:2022-04-02 03:25:36
[TOC]
## 概述
```
a = 1;
window.a // 1
```
## window.getSelection() 返回用户选中的文字
```
var selObj = window.getSelection();
var selectedText = selObj.toString();
```
## window.requestAnimationFrame() 下次重绘时候执行
推迟到浏览器下一次重流时执行,重绘通常是 16ms 执行一次.
某个函数会改变网页的布局,一般就放在`window.requestAnimationFrame()`里面执行
`window.requestAnimationFrame(callback)
`
格式:
```
function demo() {
//code
window.requestAnimationFrame(demo)
}
window.requestAnimationFrame(demo)
```
demo:
```
// 重绘代价高
function doubleHeight(element) {
var currentHeight = element.clientHeight;
element.style.height = (currentHeight * 2) + 'px';
}
all_my_elements.forEach(doubleHeight);
// 重绘代价低
function doubleHeight(element) {
var currentHeight = element.clientHeight;
window.requestAnimationFrame(function () {
element.style.height = (currentHeight * 2) + 'px';
});
}
all_my_elements.forEach(doubleHeight);
```
## window.requestIdleCallback() 推迟到系统空闲执行
';