I have a function animate_images running on some of my website's pages that performs a certain animation.
The problem I'm experiencing is that when a user navigates to the same page again, the function is called for the second time, in addition to its already running instance.
Is there a way to do something like this:
if ! (animate_images) {
animate_images();
}
Essentially, I want to make sure that animate_images never runs more than once, no matter how many times it is actually called.
is_runningproperty, set ittrueat the start of theanimate_images()function and then set to tofalsewhenever the interval / timeout is cleared / the animation is stopped.new), then you can use a "static" property of your function object, as inanimate_images.is_running. I can't remember offhand - that might technically makeanimate_images.is_runninga global (or in the same scope asis_running), as opposed to actually being a property ofis_running, but the effect should be the same. If you were instantiatinganimate_imageswithnew, then you would usethis. IIRC in Javascriptthisis scoped to the "closest" function instance.