0

Use setInterval to slide images once every one second.

setInterval(function() { slideImage();}, 1000);

When user switch to another browser tab, and comes back to the tab after some time(e.g., 5 minutes), the images slide once every 100 milliseconds. The setInterval function execution is actually queued when the browser tab is inactive.

How to stop the slide function when the image slider is invisible?

eastwater
  • 4,624
  • 9
  • 49
  • 118
  • `setInterval` returns a handle, you can stop the interval by calling `clearInterval(...)`. – Matthew Jul 06 '22 at 17:47
  • Does this answer your question? [Stop setInterval](https://stackoverflow.com/questions/16437173/stop-setinterval) – Jared Smith Jul 06 '22 at 17:47
  • ...and/or [this](https://stackoverflow.com/questions/19669786/check-if-element-is-visible-in-dom). – Jared Smith Jul 06 '22 at 17:47
  • 2
    Try the visibility API. It triggers when the user changes a browser tab. Maybe you can listen to that event and do something. – Visakh Vijayan Jul 06 '22 at 17:48
  • where is visibility API? need some callback when image slider visibility changes. – eastwater Jul 06 '22 at 17:56
  • [google is your friend](https://www.google.com/search?q=mdn+visibility+api&oq=mdn+visibili&aqs=chrome.1.69i57j35i39j0i512l3j0i22i30l5.4406j0j7&sourceid=chrome&ie=UTF-8) note though that's for *page* visibility not element visibility. You can use the second dupe link I posted for elements. – Jared Smith Jul 06 '22 at 17:57
  • Page visibility works. But there is no element visibility change events. Thanks. – eastwater Jul 06 '22 at 18:49

0 Answers0