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 pageif the window width was
>= 768px
and goes< 768px
I want to reload the page onceif the window width was
< 768px
and goes>= 768px
I want to reload the pageif 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.
source to share
// 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);
}
}
}
source to share