简体   繁体   中英

Why does my JavaScript onresize fire twice?

I have a fiddle here:

http://jsfiddle.net/fJMe9/

window.onresize = function (e) {
    console.log("Page resized");
};

And every time I resize the window I get two logs to the console

It's a well known bug (perhaps relating to event bubbling? I say well known, but that's other people who know it, not me :P ). Use a setTimeout to check the last time the window was resized to avoid this.

Try:

window.onresize = function (e) {
    console.log(e);
};

you'll see the event fires every time you drag the browser window

Depends on implementation: maybe 2 times, the first for tell you the window is being resized and the second for windows finished resizing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM