gpt4 book ai didi

javascript - 当应用程序处于后台时存储时间戳。 react native

转载 作者:行者123 更新时间:2023-11-29 20:51:46 27 4
gpt4 key购买 nike

已编辑:我想在 native 应用程序进入后台时存储时间戳,以查看是否需要重新登录。在 appstate 功能的帮助下,我使用异步存储在设备上保存时间戳。但是 getItem 和 setItem 代码永远不会执行。知道为什么吗?

componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState: any) => {

if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
this.getTime();
this.checkTimeStamp();
console.log(this.state.time, 'time var');
console.log('app is active, and has been in the background');
} else if (
this.state.appState == 'active' &&
nextAppState == 'active'
) {
this.setTime();
this.checkTimeStamp();
console.log(this.state.time, 'time var');
console.log('app is active, and just opened.');
} else {
this.setTime()
//sets the timestamp
console.log('app in background or closed');
}
this.setState({ appState: nextAppState });
};

async setTime() {
let seconds = this.generateTime();
try {
const val = await AsyncStorage.setItem('time', seconds.toString());
this.setState({ time: Number(val) });

} catch (error) {
console.error('onRejected function called: ' + error);
}
}

async getTime() {
let please = await AsyncStorage.getItem('time');
this.setState({ time: Number(please) });
console.log(this.state.time, 'time var');

最佳答案

看看 AppState https://facebook.github.io/react-native/docs/appstate它允许您监听“事件”/“后台”状态变化。

关于javascript - 当应用程序处于后台时存储时间戳。 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51481085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com