RN中的几种定时器

来源:互联网 发布:网络推广提成方案 编辑:程序博客网 时间:2024/06/10 03:28

第一类,加入定时器中的任务可能会延迟当前正在进行的动画。
1、setTimeout, clearTimeout
2、setInterval, clearInterval
3、setImmediate, clearImmediate
4、requestAnimationFrame, cancelAnimationFrame
使用:在需要的地方或者在componentDidMount 中用前面的方法(分别是setTimeoutsetIntervalsetImmediaterequestAnimationFrame)开启定时器,通常是:

componentDidMount() {    this.timer = setTimeout(      () => { console.log('把一个定时器的引用挂在this上'); },      500    );  }``` 把定时器的引用挂在this上。注意,在组件被销毁的时候晴空定时器:<div class="se-preview-section-delimiter"></div>

这里写代码片
“`

componentWillUnmount() {    // 如果存在this.timer,则使用clearTimeout清空。    // 如果你使用多个timer,那么用多个变量,或者用个数组来保存引用,然后逐个clear    this.timer && clearTimeout(this.timer);  }

requestAnimationFrame():在一段时间内只执行一次,在每帧刷新之后执行。
setTimeout():只执行一次,会尽可能快的执行。
setImmediate():只执行一次,立即执行。
setInterval():每隔设定的时间便会执行一次,不是立即执行。

第二类,在稍后执行,不会延迟当前进行的任务。

InteractionManager.runAfterInteractions(() => {   // ...需要长时间同步执行的任务...});
0 0
原创粉丝点击