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

  • Home
  • SEARCH
  • 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 8800487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:36:04+00:00 2026-06-14T00:36:04+00:00

I have an ajax call that should be executed when and only when the

  • 0

I have an ajax call that should be executed when and only when the certain flag is set somewhere else in the code.

I cannot make this call directly in the function that sets the flag because I do not need to make this call each time when flag is set.

It is a bit hard to explain so the best illustration I can think of is something like:

The wolf can eat the Red Hat when and only when she is in her grandma’s house.
However not every time she comes to this house, there is a wolf to eat her. From the other side each time when she is here and the wolf is here, the wolf will eat her.

I wonder, can I use $.when(theFlag) for this purpose?

var theRedHatIsHere = false;

function waitForRedHat()
{
.....
   theRedHatIsHere = true;
}

function wolfIsHungry(link)
{
   $.when(theRedHatIsHere)
   {
       $.ajax({
                type: "POST",
                url: "php/eatredhat.php?",
                data: ddd,
                async: false,
                success: function(msg){
                        console.log( "Eaten" );
                        window.location.href = link;
                }
        });                         
   }
}
  • 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-14T00:36:05+00:00Added an answer on June 14, 2026 at 12:36 am

    Use custom events or Publish/Subscribe pattern instead of bending $.when to do something that is not supposed to do. It is a well known pattern, it helps decouple your components and you have the flexibility for example to have multiple subscribers to the event, stop listening to the event at some point or reacting to it only once.

    Subscribe to a custom event with jQuery.on

    $(document).on('WolfIsHome', function(){
       $(document).on('WolfGotHungry', function(){
           $(document).on('RedHatArrived', function()
                $.ajax({
                    type: "POST",
                    url: "php/eatredhat.php?",
                    data: ddd,
                    async: false,
                    success: function(msg){
                            console.log( "Eaten" );
                            window.location.href = link;
                    }
                });
          }
      });
    });
    

    whenever the wolf gets hungry, instead of setting that boolean value just raise that custom event:

    $(document).trigger("WolfIsHome"); // trigger in your logic when the wolf is home
    
    $(document).trigger("WolfGotHungry"); //trigger wolfhungry which will register a subscriber to RedHat arrival event
    
    //and somewhere else in the code
    // if RedHatArrived and Wolf is not hungry then nothing will happen 
    // because there won't be registered subscribers to her arrival
    $(document).trigger("RedHatArrived"); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that is part of an $.ajax call to MVC. Two functions
I have an ajax call that I'm making to the controller which should update
I have an ajax call that fires multiple times but is called once. function
I have a jQuery ajax call that returns html of a table. Now I
I have a jQuery AJAX call that I will need to do multiple times
I have an $.ajax call that includes both a success and error condition: $('input[name=StateName]').live('change',
A situation I ran across this week: we have a jQuery Ajax call that
I have some html returned from an ajax call that contains blocks of html.
I have a ajax call on my onkeyup event, that checks if the given
I have a php script that gets called via an ajax call. Values are

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.