I ran the below code
$(document).ready(function() {
var ivar = 0;
$(window).resize(function() {
console.log('$(window).height() ' + $(window).height() + ' - ' + ++ivar);
});
});
whenever i resize i found the event was firing twice i.e. the counter 'ivar' is incremented twice.
Can any one advise me whats happening in the resize event that makes the counter to get incremeted twice
**Edit: 1. I'm resizing the window by double-clicking on the window bar.
Thanks
Definition and UsageThe onresize event occurs when the browser window has been resized. Tip: To get the size of an element, use the clientWidth, clientHeight, innerWidth, innerHeight, outerWidth, outerHeight, offsetWidth and/or offsetHeight properties.
The resize event occurs when the browser window changes size. The resize() method triggers the resize event, or attaches a function to run when a resize event occurs.
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.
That's the very well known problem. In some browsers resize is called twice. We can create timer so it will call our function only when the user stops resizing the window. Here's how you can do it:
var globalTimer = null;
$(window).resize(function() {
clearTimeout(globalTimer);
globalTimer = setTimeout(doneResize, 500);
});
function doneResize(){
console.log('$(window).height() ' + $(window).height() + ' - ' + ++ivar);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With