Not sure how to use addEventListener to watch for media query

Viewed 43

I am trying to run a function when the window is resized and hits a certain size. The following was working perfectly:

var mediaQuery = window.matchMedia("(max-width: 480px)")
mobileSlider(mediaQuery) // Call listener function at run time
mediaQuery.addListener(mobileSlider) // Attach listener function on state changes

However, I got a warning that addListener is deprecated, so I'm attempting to change it without much success. I have tried the below, but doesn't work. What should I be doing?

var mediaQuery = window.matchMedia("(max-width: 480px)")
mobileSlider(mediaQuery) // Call listener function at run time
window.addEventListener('resize', mobileSlider) // Attach listener function on state changes

I have also tried window.addEventListener('resize', mobileSlider(mediaQuery)) without success.

1 Answers
Related