r/javascript • • Aug 21 '18

Effectively supported in all browsers, JavaScript has its own form of media queries just like CSS.

https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
465 Upvotes

114 comments sorted by

View all comments

Show parent comments

6

u/backlashsid Aug 21 '18

Like what?

39

u/PortablePawnShop Aug 22 '18

Clumsily using SetInterval to track window.innerwidth to notify JS of the corresponding CSS media query and sloppily try to fasten JS and CSS together, and more I probably shouldn't admit lol. Self-taught, program recreationally

41

u/lamb_pudding Aug 22 '18

Ho boy. I imagine you've found the window resize event useful these days?

6

u/roger_comstock Aug 22 '18

If you go this route, don't you end up with thousands of unnecessary resize events?

Is this advice from Mozilla still relevant? https://developer.mozilla.org/en-US/docs/Web/Events/resize#Examples

Edit: Added link

12

u/fucking_passwords Aug 22 '18

There’s definitely a way to detect when the page has stopped resizing.

Also, debounce.

7

u/DOSMasterrace Aug 22 '18

Debounce, baby.