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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:25:09+00:00 2026-06-15T15:25:09+00:00

Question: Is there any way to pause javascript until after ajax has finished executing

  • 0

Question:

Is there any way to pause javascript until after ajax has finished executing its success function?

Relevant Code:

function validateinput (name, parent, access)
{
    //we assume that the input does not exist
    exists = false;
    //validate with server, expected response is true or false
    $.post("AJAXURL", {action: "category", name: name, parent: parent}, 
    function (data, textStatus, xhr)
    {
        //if returned true, the entry does exist for that parent
        if (data == "true")
        {
            alert("AJAX");
            exists = true;
        }
        else
            exists = false;
    });

    switch (true)
    {
        //If the name field is blank (still displaying hint)
        case (name == 'Subcategory Name' || name == 'Category Name'):
            alert("Please enter a name");
            break;
        //if the name already exists for that parent
/*****/     case (exists == true):
            alert("SWITCH");
            break;
        //if the parent field is displaying hint, assume parent is blank
        case (parent == 'Parent Category'):
            parent = '';
            break;
        //if the usergroup field is blank, assume value of parent
        case (access == 'UserGroup Allowed To View'):
            //if parent is also blank, assume lowest level usergroup access
            if (parent == '')
                access = "UserGroupTicketWorkers";
            else
                    $.post("AJAXURL", {action: "specificcat", name: parent}, 
                function (data, textStatus, xhr)
                {
                    access = data['access'];
                }
                );
            break;

        default:
            return true;
    }
    return false;
}

Detailed Explanation Of Issue:

  • User clicks a link in page to fire this function.
  • function will return true or false whether there is an error with the user input
  • When this code executes, I receive alert("SWITCH"); BEFORE I receive alert("AJAX"); (the condition of the second case is currently correct, I inverted it in my debugging to figure out what was happening)
  • I have a temporary fix for it which is moving the second case directly before the default, but my guess is that a faster computer may execute the switch comparisons faster than the server will provide a response, and therefore not a permanent solution. (unless it doesn’t work that way), and that if I put in a timer to wait for a preset amount of time, that the same issue could occur if the server is running slower than normal
  • I’m aware that I’m not getting the new value from the ajax call because there is the time spent communicating with server, so, my first thought is to find a way to pause the function until the ajax success function is completed.
  • I cannot put the switch inside the ajax call since this function needs to return a value.

Resolution

I used the async setting and it worked like a charm. Just had to change my code from $.post() to $.ajax() and set it up to post

$.ajax(
{
    url: "plugins/actions/edittickets/libs/changetickets.php", 
    type: 'POST',
    data: 
    {
        action: "category", 
        name: name, 
        parent: parent
    }, 
    async: false,
    success: function (data, textStatus, xhr)
    {
        //if returned true, the entry does exist for that parent
        if (data == "true")
            exists = true;
    }
}); 
  • 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-15T15:25:10+00:00Added an answer on June 15, 2026 at 3:25 pm

    You can send your AJAX query in a synchronous way if that’s what your really want by adding 'async':false in your list of parameters.

    See the documentation for more info : http://api.jquery.com/jQuery.ajax/

    But as other people pointed out in comments this is NOT how switch works so you still have to understand the switch statement et change the corresponding code. See here for more info : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/switch

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

Sidebar

Related Questions

there's a question: Is there any way to pair Bluetooth device in Windows programmatically?
Simple question here: is there any way to convert from a jagged array to
My question is very simple: is there any way to (programmatically, technically, or manually)
My question is, Is there any way or is it possible to call Android
Sorry for this silly question, but is there any way to restrict using directives
Weird question, but i need to find an answer. Is there any way, in
Simple question really, is there any programmatic way to over load the choose a
This is a simple question : Is there any automatic indentation alignment on Eclipse
Just a curious question but is there any programs that can help/aid you when
is there any faster way to parse a text than by walk each byte

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.