serakfalcon
serakfalcon

Reputation: 3531

window.location.reload(true) working inconsistently

I've been working on an automatic log-out functionality for my web page. As a part of that, I implemented the window.location.reload(true) in 3 different places in my code. Two of them are automatic, one is attached to a link. The link always works, but the automatic ones don't always work, and I don't understand why.

For the automatic logouts, one set by a debouncer:

var userActionTimeout = debounce(function(e) {
    console.log("inaction timeout, reloading page");
    window.location.reload(true);
},15000;);
$(document.body).on('mousemove keydown click scroll',userActionTimeout);

Which theoretically should reload the page after a certain amount of inactivity.

The other two uses happen after certain types of AJAX data submission (e.g. blatantly wrong data sent that could only happen if the client was modified) trigger a log out. Of course, any further AJAX submissions are ignored by the server, and the next page the server will serve the client is a login page. In the event this happened inadvertently, AJAX sends the client an error message that includes the following:

<a href="#" onclick="window.location.reload(true);">refresh</a> to continue session

I also implemented a timeout that also happens if this link is served, which happens after the AJAX response is received:

if (typeof response._forceRefresh !== 'undefined' && response._forceRefresh) {

            console.log('reload firing');

            /*
             some code to insert the link into a spotlight here
            */
            setTimeout(function(){console.log('reloading in 3s...');},7000);
            setTimeout(function(){
                console.log('reloading...');
                window.location.reload(true);
            },10000);
} 

However the issue I'm having is this: Most of the time, the debounce page reload works (tested in firefox & chrome), however occasionally it doesn't. The link always works, but the AJAX response reload is about 50/50. I know it receives the response from the server since the link shows up, but quite often it doesn't actually automatically reload the page.

What is going on?

Upvotes: 4

Views: 3434

Answers (1)

Cindy
Cindy

Reputation: 26

When ever I get inconsistency on a web page, it usually involves caching that I didn't realize was happening. If you haven't already, look through your project with that in mind and see if there is an effected location that you can force it not to cache a page.

Another idea might be to try using the meta refresh element. There is another thread where this is suggested: auto logout idle timeout using jquery php

Upvotes: 1

Related Questions