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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:29:04+00:00 2026-06-10T08:29:04+00:00

In some places we have to have only one instance of function running. This

  • 0

In some places we have to have only one instance of function running.

This code works for me:

function example() { 
    var that = this; 
    if(that.running) { 
       return false; 
    }
    that.running = true; 
    $.get(url, {}, function (data) {
         that.running = false; 
    }); 
}

How we can improve it, and make it more reusable?

UPD Here is solution, based on Frits van Campen answer:

function make_run_once(callback) {
    callback.running = false;
    return function () {
        if(callback.running) {
            return false;
        }
        callback.running = true;
        deferred = $.Deferred();
        deferred.done(function () {
            callback.running = false;
        });
        callback(deferred);  // pass deferred to callback so it can resolve at it's own leisure
    };
}
  • 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-10T08:29:05+00:00Added an answer on June 10, 2026 at 8:29 am

    You might want to use jQuery’s `Deferred’ functionality for that:

    function decorate(fn) {
        var deferred;
        return function(){
            if (!deferred) {
                deferred = new jQuery.Deferred;
                fn(deferred.resolve); // pass in the callback
            }
            return deferred;
        };
    }
    

    A decorated function will always return the same Deferred object, which will be resolved with the callback from the original function – started on the first invocation.

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

Sidebar

Related Questions

I am trying to insure that only one instance of a perl script can
I have some repetitive code that is used by a few of my actions
I need to make sure that user can run only one instance of my
I'm working on a class-based php web app. I have some places where objects
I have a Singleton to manage some variables I need in various places in
I have some data that I want to store somewhere in my Rails app
I have a lot of code that performs bitwise operations on unsigned integers. I
I am wondering how you would approach this problem I have two Taxrates that
Let's say I have some class X that is going to be injected all
I'm on an ASP.Net 2.0 project, in C#. I have some data that gets

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.