首页 > InteractionManager.setDeadline接口的作用是神马呢?

InteractionManager.setDeadline接口的作用是神马呢?

没搞懂 InteractionManager.setDeadline(time) 的作用是什么,文档说的较抽象,本以为它会推迟 InteractionManager.runAfterInteractions 的执行时间点结果发现并非如此,参见下方代码:

class AwesomeProject extends Component {
    constructor(props) {
        super(props);
        this.state = {
            width : '',
            height : ''
        };
    }
    componentDidMount() {
        var me = this;
        var handle = InteractionManager.createInteractionHandle();

        InteractionManager.clearInteractionHandle(handle);

        InteractionManager.setDeadline(5000);  //啥都没做!!!!

        InteractionManager.runAfterInteractions(() => {
            var win_info = Dimensions.get('window');
            me.setState({
                width: win_info.width,
                height: win_info.height
            })
        });
    }
    render() {
        return (
            <View style={styles.container}>
                <Text style={styles.text}>
                    width:{this.state.width || 'loading'}
                </Text>
                <Text style={styles.text}>
                    height:{this.state.height || 'loading'}
                </Text>
            </View>
        );
    }
}

只能赐教各位大大,这个接口作用到底是什么呢?能给个例子就更好了么么哒


不可能什么都不做,你自己多打日志,打印时间值,比较各个日志的时间,肯定有不同。


根据来自React Native中文网的文档

static setDeadline(deadline: number)

如果设定了一个正整数值,则会使用setTimeout来挂起所有尚未执行的任务。在eventLoopRunningTime到达设定时间后,才开始使用一个setImmediate方法来批量执行所有任务。

另外前面有一段描述:

默认情况下,排队的任务会在一次setImmediate方法中依序批量执行。如果你调用了setDeadLine方法并设定了一个正整数值,则任务只会在设定的时间到达后开始执行。在此之前,任务会通过setTimeout来挂起并阻塞其他任务执行,这样可以给诸如触摸交互一类的事件留出时间,使应用可以更快地响应用户。

我理解的意思是:当其它的动作/动画运行结束后,正常情况下会立即开始执行排队的任务。但如果你设定了这个值,则还可以给用户留出一些时间来继续返回操作。

【热门文章】
【热门文章】