我有一个需要一直留在的应用程序。为了阻止iPad在不活动5个小时后关闭/睡觉,我将每4小时重新加载主屏幕。但是,如果用户使用键盘,我不希望它刷新。我的应用程序令人耳目一新,但我无法停止键盘didshow侦听器上的计时器。这是我的代码:
componentDidMount(){
this.keyboardDidShowListener =
Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
this.keyboardDidHideListener =
Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
this.timeoutHandle = setTimeout(()=>{
this.props.navigation.navigate('Home');
},30000);
}
_keyboardDidShow () {
console.log("Keyboard Shown")
if (this.timeoutHandle) {
clearTimeout(this.timeoutHandle);
this.timeoutHandle = 0;
}
};
componentWillUnmount(){
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
clearTimeout(this.timeoutHandle);
}
_keyboardDidHide () {
console.log('Keyboard Hidden');
}
我尝试了许多不同的方法,但是无法在键盘迪德霍克上清除计时器。如果可以的话,请提供帮助。
您的功能在错误的上下文中被执行。
这些行:
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
需要对此进行更改:
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this));
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide.bind(this));