简体   繁体   中英

Why this polling stops working after idling for some time?

I am using AngularJS to constantly poll for new data through HTTP POST. An alert will be sent when new data is received. The code which is inside a controller looks something like this;

    var poll = function() {
        $http.get('phones.json').success(
            function(data)
            {
                new_val = data.val;
                if ( (new_val!== old_val) )
                {
                    $window.alert("AlertEvent");                        
                }

                old_data = new_val;
                $timeout(poll, 500);
            }
        );
    };
    poll();   

This code works when the html page is refreshed. Working means when phones.json is changed, an alert will appear. However, if I leave the page on for, say 30 minutes, and come back later, it stops working. I have to refresh the page to get it working again.

What else did I miss out? What did I do wrong? Could it due to some caching mechanism?

Thank you very much.

EDIT: I found the cause. It is indeed due to the browser reading from cache. I can see this using Chrome Developer tools. How can this caching be disabled for this html page only?

You may be able to bust the cache by doing something like this:

 $http.get('phones.json?v=' + Date.now())

Depending on how your back-end is set-up you may need to adjust it to accept that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM