0

I am facing a problem whenever I resize the browser window by (CTRL+SHFT+M) or change the orientation of window. counter_ variable value increament by 4 or 7 instead of 1.

HTML:

<div id="my_element">
    <span id="text">0</span> window resize.
</div>

Script:

$(function () {
    var counter_ = 0;
    $(window).on('resize', function () {
        $('#text').text(++counter_);
    });
});

Output Should be:

0 window resize.

1 window resize.

2 window resize.

..

..

Mohith P
  • 585
  • 5
  • 14
Nits
  • 520
  • 6
  • 21
  • 4
    The `resize()` event is called once for every pixel the window is changed by. You may need to use a timer to debounce the effect if you only want to count an entire resize as one operation. – Rory McCrossan Feb 08 '16 at 13:29
  • [JavaScript/JQuery: $(window).resize how to fire AFTER the resize is completed?](http://stackoverflow.com/q/2854407/387194) – jcubic Feb 08 '16 at 13:34
  • @RoryMcCrossan not for every pixel, for every repaint. If your browser decides to not repaint the page during resize, you might end up with one resize event only. OTOH, you can easily get multiple resize events for the same window size. – John Dvorak Feb 08 '16 at 13:53
  • It worked for me. Thanks. – Nits Feb 09 '16 at 06:33

1 Answers1

-1

Try:

$('#my_element').append('<span class="text">' + (counter_++) + '</span> window resize.');

As in this FIDDLE

jcubic
  • 61,973
  • 54
  • 229
  • 402