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.

Community
  • 1
  • 1
Marian Rick
  • 3,350
  • 4
  • 31
  • 67
  • http://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-of-resize-event-and-only-then-perform-an-ac – Skatch Jul 06 '15 at 09:13
  • Why is it the page needs to reload? If it's a styling issue, you may want to look into Media Queries – Jonathon Blok Jul 06 '15 at 09: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! – Marian Rick Jul 06 '15 at 09:20

1 Answers1

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);                
        }
    }
}
Jonathon Blok
  • 749
  • 4
  • 14