Vue 页面10分钟无操作时返回首页

251 篇文章 18 订阅

这种需求手机端和pc端一般是不存在的,因为都是可以手动操作刷新的。

最近在做一个户外社区大屏的项目,因为大屏是全屏显示,没法手动刷新,不可能在页面专门做一个刷新按钮,也不好看,那这样的需求就显得格外重要了。

首先我们来分析一下需求:

  1.15分钟——需要定时器

  2.无操作——监控页面上的点击、触摸、滑动等事件

  3.返回首页——切换路由

我们只需要设置一个定时器,在一进入页面的时候就开始计时,如果15分钟内有点击、触摸、滑动等操作时就重新计时,时间一到就切换路由。

而且我们还需要新建一个空白组件rbck.vue(路由名字随意),切换时先跳转到 /rbck  ,在rbck.vue里立即执行跳转到首页,达到重定向并刷新的效果。

在main.js里

配置路由

import rbck from './components/rbck.vue'
const routes = [

  {

    path: '/rbck',

    meta: {

      title: '跳转页',

      scrollToTop: true

    },

    component:rbck,

  }

]

  

  

data() {

    return {

      timeOut: ''

    }

  },
methods: {

    //页面15分钟无操作时返回首页

    startTimer() {

      let that = this;

        clearInterval(that.timeOut);

        that.timeOut = setInterval(function () {

          that.$router.push({path: '/rbck'})

        },1000*60*15)

    },

    isTimeOut() {

      let that = this;

      if(that.$route.path == "/") {

        that.startTimer();

      }

      document.body.onmouseup = that.startTimer;

      document.body.onmousemove = that.startTimer;

      document.body.onkeyup  = that.startTimer;

      document.body.onclick  = that.startTimer;

      document.body.ontouchend  = that.startTimer;

    },

}

 

解决跳转之前路由等于跳转之后路由问题:

watch: {

    '$route' (to, from) {

      if (to.path == from.path) {

        this.$router.push({

          path: '/rbck'

        })

      }

    }

  },

rbck.vue代码如下:

<script type="text/ecmascript-6">

    export default{

        data(){

            return{



            }

        },

        created () {

          this.backFun();

        },

        methods: {

          backFun() {

            this.$router.replace({path: '/'})

          }

        },

        components:{



        }

    }

</script>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值