Reputation: 3440
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 pageif window width was
>= 768px
and goes< 768px
I want to reload the page onceif window width was
< 768px
and goes>= 768px
I want to reload the pageif 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.
Upvotes: 3
Views: 2184
Reputation: 749
// 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);
}
}
}
Upvotes: 1