Detecting when a window resize event

I have a site that needs to be reloaded after resizing and found its own coding limit script that automatically reloads the page:

I want the script to behave like this:

if the window width was < 768px

and remains < 768px

I do not to reload the page

if the window width was >= 768px

and goes < 768px

I want to reload the page once

if the window width was < 768px

and goes >= 768px

I want to reload the page

if the window width was >= 768px

and remains >= 768px

, it should always reload

The last part is simplified 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 need to create var

one that stores the current $(window).width();

one and then validates with if {} else if {} else {}

, but from this point on my mind loses control.

+3


source to share


1 answer


// 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);                
        }
    }
}

      



+1


source







All Articles