猿问

如何设定在指定的时间点执行功能

状况是这样的


现在要处理token过期的问题,所以想要在一个指定时间(expired_in)前五分钟刷新token


而api回传的值是Tue Nov 21 2017 00:40:59 GMT + 0800(CST)

这样的格式


但印象中的setTimeout只能输入时间间隔(ms)


所以目前适用watch监看expired_in这个值


只要这个值有变动就呼叫setToken api


     watch:{

         tokenExpiredIn(){

           this.refreshToken = setTimeout(()=> {

             this.setToken()。then(expires => {

               this.tokenExpiration = expires.valueOf() - Date.now()。valueOf() - 300000 // format to ms & expired_in + 50mins

            })

           },this.tokenExpiration)

        }

      }

      

      

  但这样依然无法完全指定在expired_in前的五分钟...


不知道有没有什么更好的思路


jeck猫
浏览 431回答 1
1回答

绝地无双

setTimeout(()=>console.log('token will be expiried in 5 mins'), new Date(Date.parse("Tue Nov 21 2017 00:40:59 GMT +0800 (CST)"))-Date.now()-5*60*1000)
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答