How to create Semaphore between HTML elements loaded async
I have in an HTML page, an element that appears several times, and running the same JS. Problem is, I want it to do a specific function only if it was the first one to run it (his siblings never ran it - YET). I need semaphore to sync between them. I am unable to know how to declare a variable and to do semaphore in this way in JS. There are lots of approaches. You need to put a flag somewhere . In the absence of anything else, you can put it on window , but use a name that's unlikely to conflict with anything else. Then the JavaScript is quite straightforward: if (!window.myUniqueNameFlag) {