I am doing something on window resize in VUE, and wrote a function which will give me the window width after resize and just before resize, I.e. old window size and new window size,
So when I am resizing the window, on very first resize its giving the old value undefined, and from 2nd resize everything is working fine, What's wrong with it that on first instance windows old value is undefined. while I am calculating it on load (mounted) as well.
Here is the working demo
TO check the issue: resize the result window and check console, on first resize it will give old value undefined, but when you resize it again, it will work fine.
methods: {
getWindowWidth(){
var winWidth = window.innerWidth;
var newWinWidth = window.innerWidth;
console.log("initial width " + winWidth);
var doIt;
},
handleResize() {
const _this = this
//_this.mounted();
clearTimeout(_this.getWindowWidth.doIt);
_this.getWindowWidth.doIt = setTimeout(function() {
_this.getWindowWidth.winWidth = _this.getWindowWidth.newWinWidth;
_this.getWindowWidth.newWinWidth = window.innerWidth;
console.log("new " + _this.getWindowWidth.newWinWidth);
console.log("old " + _this.getWindowWidth.winWidth);
//console.log("old "+ _this.winWidth);
if(_this.getWindowWidth.newWinWidth > 1025 && _this.getWindowWidth.winWidth < 1025){
console.log("refresh now");
//window.location.reload();
}
if(_this.getWindowWidth.newWinWidth < 1025 && _this.getWindowWidth.winWidth > 1025){
console.log("refresh again");
//window.location.reload();
}
}, 1000);
}
},
mounted: function() {
const _this = this
window.addEventListener('resize', _this.handleResize);
}