angular.js - How to determine whether to readjust the interface or adjust the content of localstorage based on time in angular
phpcn_u1582
phpcn_u1582 2017-05-15 17:10:16
0
1
571

I hope to be able to use time to determine whether the data is retrieved through the interface again or directly retrieved from localstorage. However, the interface returns a promise object. If you use if else to determine whether it will report promise.then is not a function .

phpcn_u1582
phpcn_u1582

reply all(1)
洪涛

Take a time point after the page is loaded, and then take the time when you click refresh. Look at the time difference to determine whether to use local or interface. The logic of the two is different, so they need to be called in different ways. Finally, assign the value to the variable that stores the data on the page, so that the error you mentioned will not occur.
Supplement:
For example, your page data is stored using $scope.data.
Go to the time point when loading the page and store it in the start variable.
When you click refresh, get the end time and save it as end. You can also get the time difference directly. If the time difference is greater than or equal to one minute, retrieve the data from the interface. Otherwise, get data from localStorage.
The interface retrieves data and returns a promise, while localStorage returns a string.

if(end-start>= 1){ //从接口取数据,加入接口名字为DataService
    DataService().then(function(response){
        console.log(response); //这里默认接口回来的数据和你页面数据是一样的,有可能页面数据是接口数据的一部分,需要用点进行调用,例如 response.data
        $scope.data = response;
    })
}else{
    //从localStorage取数据,这里假定你存入的数据名为data
    //取回来的是字符串,需要转成json。注意,存入的时候也要是字符串,需要用JSON.stringify()转换
    $scope.data = JSON.parse(localStorage.getItem("data"));
}

The code only displays logic and cannot be run directly.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template