Please find below my code:
<script type="text/jscript">
$(document).ready(function() {
$(window).resize(function(){
alert("Window resized");
});
});
</script>
Whenever the window is resized i get this alert() run twice. Why?
Please find below my code:
<script type="text/jscript">
$(document).ready(function() {
$(window).resize(function(){
alert("Window resized");
});
});
</script>
Whenever the window is resized i get this alert() run twice. Why?
It doesn't just fire twice, it will fire any number of times when the window is resized. Firefox used to just trigger a resize event when the resize was "done" but now it triggers multiple resize events during the resize, just like Chrome has always done.
So you need to make the resize event callback as small as possible since it will fire often. You could throttle it by doing something like this:
var resizeTimer = undefined;
$(document).ready(function() {
$(window).resize(function() {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function () {
alert("Window resized");
}, 200);
});
});
This will only call alert
if no subsequent resize event comes in in less than 200 ms.