Skip to content
Advertisement

setInterval Overlapping DOM changes

I’m using setInterval to iterate through some images on a page and hide/show the next in a list after x seconds have passed. Every 30 seconds, I make a GET request to check for new images from my server. Because the http request takes about a second, setInterval begins executing the next iteration of my code which causes things to get a little screwy. What would be the best way to fix this issue? Here is a sample of my code:

JavaScript

Advertisement

Answer

Something like this.

JavaScript

OR , use a flag and simply return if sync is busy.

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