Skip to content
Advertisement

How to clear setinterval() when resizing and inside condition Using Jquery

I’, trying to run this functin only on mobile devices which is less than 768px , it works on this screen width but it also ignore the condition and works on larger screens than 768px, i tried to clear interval but there is something wrong with my code

JavaScript
JavaScript
JavaScript

Advertisement

Answer

Based on your code I moved things around to factor behaviours in functions. So now you have the functions startAnimation and stopAnimation that will be triggered by the resize event handler based on conditions.

But the key was also keeping track of the fact the animation was running or not.

So when you resize the window and the size goes < 768px (and the animation is not already running) triggers the beginnning of the animation.

When you keep resizing the window, the animation will not trigger again if it’s running already. But if the width goes >= 768px the animation will stop and will be ready to restart as soon as the window width will go lower than the condition.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement