一只老猿
一只老猿
  • 发布:2023-12-13 11:38
  • 更新:2024-01-09 01:37
  • 阅读:152

记录APP一次使用的累计时长

分类:uni-app

uniapp vue APP
老板提了一个新需求:
用户使用30分钟后,弹出 “使用时间太久啦,眼睛休息一下” 的弹框。

如何实现,一点思路没有,有大神帮帮忙指导一下吗?

2023-12-13 11:38 负责人:无 分享
已邀请:
套马杆的套子

套马杆的套子 - 没有解决不了的问题,只有解决不完的问题

app.vue里,onShow onHide

  • 一只老猿 (作者)

    嗯 好像也只能这样。谢谢

    2023-12-13 12:00

  • 一只老猿 (作者)

    大神,我的实现方法有问题,能帮忙看看代码,指导一下如何修改吗?万分感谢

    2023-12-23 10:48

一只老猿

一只老猿 (作者)

//App.vue
<script>
export default {
globalData: {
secondCount: 0,
},
data() {
return {
timer: null, // 计时器
};
},
onLaunch() {
console.log('App Launch');
},
onShow() {
console.log('App Show');
this.timer = setInterval(() => {
this.globalData.secondCount++;
}, 1000);
},
onHide() {
clearInterval(this.timer);
this.timer = null;
console.log('App Hide');
},
beforeDestroy() {
clearInterval(this.timer);
this.timer = null;
},
methods: {
watch: function(method, istr) {
var obj = this.globalData;
Object.defineProperty(obj, istr, {
configurable: true,
enumerable: true,
value:second,
set: function(value) {
this.globalData.secondCount = value;
method(value);
},
get: function(value) {
return this.globalData.secondCount;
}
})
}
}
}
</script>

//index.vue
<template>
<view>
</view>
</template>
<script>

export default {  
    data() {  
        return {                  
        }  
    },        
    onLoad() {  
        const App = getApp();  
        console.log("globalData:"+JSON.stringify(getApp().globalData));           
        getApp().watch(this.getWatchCount,"secondCount")  //sumTime是globalData中定义的属性  

    },  
    methods: {  
        getWatchCount(val){  
                console.log('输出变化的值',JSON.stringify(val));  
                //当 sumTime>10 调用方法 handleSumTimeGreaterThan10  
        },  
        handleSumTimeGreaterThan10() {  
              // 处理 secondCount 大于10的情况  
              console.log('secondCount 大于10了!');  
              // 将全局变量sumTime设置0,重新计数  
        }  
    }  
}  

</script>

<style>
</style>

8***@qq.com

8***@qq.com

websocket主动发送信息给前端,定时器轮作判断,方案二用户登陆后记录记录当前的时间并且使用定时器每秒监听当前的时间变化并用当前时间减登陆时间是否大于30即可做出提示

要回复问题请先登录注册