3

I have a site that needs to reload after resizing and have found my own limit of coding a script, that automatically reloads the page:

I want the script to behave like follows:

if window width was < 768px and stays < 768px I do not want to reload the page

if window width was >= 768px and goes < 768px I want to reload the page once

if window width was < 768px and goes >= 768px I want to reload the page

if window width was >= 768px and stays >= 768px it always should reload

The last part is done easy using the following code:

// After resize events
var id;
$(window).resize(function() {
    clearTimeout(id);
    id = setTimeout(doneResizing, 500);
});
function doneResizing(){
    if($(window).width() > 767) {
        if (window.RT) clearTimeout(window.RT);
          window.RT = setTimeout(function()
          {
            this.location.reload(false); /* false to get page from cache */
          }, 200);
    }
}

I think I have to create a var that stores the current $(window).width(); and than check with if {} else if {} else {}, but from this point my mind looses the control.

3
  • stackoverflow.com/questions/5489946/… Commented Jul 6, 2015 at 9:13
  • Why is it the page needs to reload? If it's a styling issue, you may want to look into Media Queries Commented Jul 6, 2015 at 9:17
  • @JonathonBlok I have a few scripts, that I am not able to "remove", after I have added them, and although the site does calculate a lot of heights and widths, wich need to be recalculated, as soon as the window size changes! Commented Jul 6, 2015 at 9:20

1 Answer 1

1
// After resize events
var id;
var startWidth = window.innerWidth; //get the original screen width

$(window).resize(function() {
    clearTimeout(id);
    id = setTimeout(doneResizing, 500);
});
function doneResizing(){
    if ($(window).width() > 767) {
        this.location.reload(false);
    } else {
        if (startWidth > 767){
            this.location.reload(false);                
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.