Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403574
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:16:40+00:00 2026-05-18T05:16:40+00:00

I have in an HTML page, an element that appears several times, and running

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:16:40+00:00Added an answer on May 18, 2026 at 5:16 am

    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) {
        window.myUniqueNameFlag = true;
        // Do your processing
    }
    

    But again, putting things on window is not ideal if you can avoid it, although it’s very common practice. (Any variable you declare at global scope with var¹ is a property of window, as is any function you declare at global scope.)

    If your function is already declared at global scope (and therefore already occupying a global identifer / window property), you can do this to avoid creating a second identifer. Instead of:

    function foo() {
        // ...your processing...
    }
    

    Do this:

    var foo = (function() {
        var flag = false;
    
        function foo() {
            if (!flag) {
                flag = true;
                // ...your processing...
            }
        }
    
        return foo;
    })();
    

    That looks complicated, but it’s not really: It defines and immediately invokes an anonymous function, within which it defines a variable and a nested function, then it returns the nested function’s reference and assigns it to the foo variable. You can call foo and you’ll get the nested function. The nested function has an enduring reference to the flag variable because it’s a closure over the variable, but no one else can see it. It’s completely private.

    A third option is to just use a flag on the function object itself:

    function foo() {
        if (!foo.flag) {
            foo.flag = true;
            // ...do your processing...
        }
     }
    

    Functions are just objects with the ability to be called, so you can add properties to them.


    ¹ Variables declared at global scope with let or const are globals, but do not become properties of window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have n number of select elements in an html page that are used
I have HTML page with some HTML element with ID=logo . I need to
I have a html page that allows users to submit a file. Below is
I have an html page that open a popup window when the page loads.
I have a html page that needs to expand width and height, so needs
I have a web page that contains a div element called #order_details that is
I have this simple HTML page that consists of 4 divs of equal height
I have an HTML page. I would like to extract all elements whose name
how can i do. i have many html elements on page. after clicking on
I have the following elements in a HTML page: <body> <div style=height:100%; width:100%> <div

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.