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 8630407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:03:12+00:00 2026-06-12T09:03:12+00:00

I need to create a function which can be executed only once, in each

  • 0

I need to create a function which can be executed only once, in each time after the first it won’t be executed. I know from C++ and Java about static variables that can do the work but I would like to know if there is a more elegant way to do this?

  • 1 1 Answer
  • 0 Views
  • 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-06-12T09:03:23+00:00Added an answer on June 12, 2026 at 9:03 am

    If by "won’t be executed" you mean "will do nothing when called more than once", you can create a closure:

    var something = (function() {
        var executed = false;
        return function() {
            if (!executed) {
                executed = true;
                // do something
            }
        };
    })();
    
    something(); // "do something" happens
    something(); // nothing happens
    

    In answer to a comment by @Vladloffe (now deleted): With a global variable, other code could reset the value of the "executed" flag (whatever name you pick for it). With a closure, other code has no way to do that, either accidentally or deliberately.

    As other answers here point out, several libraries (such as Underscore and Ramda) have a little utility function (typically named once()[*]) that accepts a function as an argument and returns another function that calls the supplied function exactly once, regardless of how many times the returned function is called. The returned function also caches the value first returned by the supplied function and returns that on subsequent calls.

    However, if you aren’t using such a third-party library, but still want a utility function (rather than the nonce solution I offered above), it’s easy enough to implement. The nicest version I’ve seen is this one posted by David Walsh:

    function once(fn, context) { 
        var result;
        return function() { 
            if (fn) {
                result = fn.apply(context || this, arguments);
                fn = null;
            }
            return result;
        };
    }
    

    I would be inclined to change fn = null; to fn = context = null;. There’s no reason for the closure to maintain a reference to context once fn has been called.

    Usage:

    function something() { /* do something */ }
    var one_something = once(something);
    
    one_something(); // "do something" happens
    one_something(); // nothing happens
    

    [*] Be aware, though, that other libraries, such as this Drupal extension to jQuery, may have a function named once() that does something quite different.

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

Sidebar

Related Questions

I need to create a function which rounds decimal numbers like this: Round($32.95, 0)
I need to be able to create a seperate function which creates a total
I need to create links on webpage which call a javascript function with different
I need to create a function that uses a loop. This function will open
i need to create a function in my application to set its available memory
I need to create a function that, among other things, spawns a child process.
I need to create a function that takes month and year as parameters and
I need to create a function that receives a string and checks if the
I need to create a javascript function that checks if it has been a
I need to create a php search function for names and need to change

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.