mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 04:35:36 +08:00
Update Timers.md
This commit is contained in:
@@ -32,3 +32,33 @@ var Component = React.createClass({
|
|||||||
```
|
```
|
||||||
|
|
||||||
We highly recommend never using bare timers and always using this mixin, it will save you from a lot of hard to track down bugs.
|
We highly recommend never using bare timers and always using this mixin, it will save you from a lot of hard to track down bugs.
|
||||||
|
|
||||||
|
## InteractionManager
|
||||||
|
|
||||||
|
One reason why native apps feel so good performance wise is that barely any work is being done during an interaction/animation. In React Native, you can use `InteractionManager` that allows long-running work to be scheduled after any interactions/animations have completed.
|
||||||
|
|
||||||
|
Applications can schedule tasks to run after interactions with the following:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
InteractionManager.runAfterInteractions(() => {
|
||||||
|
// ...long-running synchronous task...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Compare this to other scheduling alternatives:
|
||||||
|
|
||||||
|
- requestAnimationFrame(): for code that animates a view over time.
|
||||||
|
- setImmediate/setTimeout/setInterval(): run code later, note this may delay animations.
|
||||||
|
- runAfterInteractions(): run code later, without delaying active animations.
|
||||||
|
|
||||||
|
The touch handling system considers one or more active touches to be an 'interaction' and will delay `runAfterInteractions()` callbacks until all touches have ended or been cancelled.
|
||||||
|
|
||||||
|
InteractionManager also allows applications to register animations by creating an interaction 'handle' on animation start, and clearing it upon completion:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var handle = InteractionManager.createInteractionHandle();
|
||||||
|
// run animation... (`runAfterInteractions` tasks are queued)
|
||||||
|
// later, on animation completion:
|
||||||
|
InteractionManager.clearInteractionHandle(handle);
|
||||||
|
// queued tasks run if all handles were cleared
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user