I have a jQuery function which changes the size of columns if some conditions are met. The function starts working once the window has been resized. But it doesn's stop recalculations once the conditions return false
.
How it could be solved?
Here is a script:
jQuery(document).ready(function ($) {
element_second = document.getElementsByClassName('second_column')[0];
element = document.getElementsByClassName('first_column_main_page')[0];
$(window).on('resize', function(){
interval = setInterval(updateHeight,3000);
});
function updateHeight(){
var $window = $(window);
var width = $window.width();
var height = $window.height();
if (height > 667 && height < 800) {
interval = setInterval(updateHeight,300);
console.log('true');
setInterval(function () {
if ((width != $window.width()) || (height != $window.height())) {
width = $window.width();
height = $window.height();
element.style.height = ((height - element.offsetTop) + "px");
element_second.style.height = ((height - element.offsetTop) + "px");
}
}, 300);
}
else {
console.log('false');
clearInterval(interval);
element.style.height = (840 + "px");//
element_second.style.height = (840 + "px");// this part makes all blocks to flash, cause recalculations dosn's stop
}
};
});
In other words, I need a script to recalculate heights only for specific dimensions.
@Titus answer:
jQuery(document).ready(function ($) {
element_second = document.getElementsByClassName('second_column')[0];
element = document.getElementsByClassName('first_column_main_page')[0];
$(window).on('resize', function(){
updateHeight();
});
function updateHeight(){
var $window = $(window);
var width = $window.width();
var height = $window.height();
if (height > 667 && height < 800) {
console.log('true');
{
width = $window.width();
height = $window.height();
element.style.height = ((height - element.offsetTop) + "px");
element_second.style.height = ((height - element.offsetTop) + "px");
}
else {
console.log('false');
}
};
});