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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:26:55+00:00 2026-05-29T12:26:55+00:00

see the example first then you can understand my question. var x; function checkTime()

  • 0

see the example first then you can understand my question.

var x;
function checkTime() {
    $.ajax({ 
        type: 'GET', 
        url: 'http://www.example.com/time.php', 
        data: test,
        dataType:'json',
        success: function (data) {
            if (data == 0){
                x = 'true';
            } else if (data == 1){
                x = 'false';
            }
        }
    });
}

checkTime();
alert (x);

the alert will be undefined

I need to set x inside the checkTime functions and grab it outside the function

even if i do :

var x = checkTime();
alert (x);

i still get undefined

also i only need to check for true or false, maybe there is another way to do things. I also tried:

...
if (data == 0){
    return true;
} else if (data == 1){
    return false;
}
...

but i don’t know how to check for that.

basically i need to so something like :

if (x == 1){ // if(x == true)
//do something
}

any ideas?

thanks

  • 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-05-29T12:26:57+00:00Added an answer on May 29, 2026 at 12:26 pm

    This happens because the default behaviour of .ajax() is asynchronous. x will not have been set by the time your alert() fires. You either need to set the .ajax() with aync=false or pass in a callback function to call when your Ajax request has completed, e.g.

    function checkTime(callback) {
        $.ajax({ 
            type: 'GET', 
            url: 'http://www.example.com/time.php', 
            data: test,
            dataType:'json',
            success: function (data) {
                var x = '';
                if (data == 0){
                    x = 'true';
                } else if (data == 1){
                    x = 'false';
                }
                // Call the callback function with 'x'
                callback.call(null, x);
            }
        });
    }
    

    And then use it like follows:

    checkTime(function (x) {
        alert(x);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't get this: http://localhost/index.php/articles/edit/1/my-first-article This URL is mentioned as an example in the
The only way I can see to introduce my question, is by providing an
First of all, this isn't another question about storing images on DB vs file
First of all, I understand that it's unusual that I want to up-convert like
First URL stands for Uniform Resource Locator. It will be very difficult to remember
first of all, this is the first week that I use swing, then sorry
First I'll explain, then I'll paste the code. I actually copied the code from
DISCLAIMER: Don't use ::feof() as your loop condition. For example, see the answer to:
I'm having trouble to understand how to use Quartz with QuartzInitializerListener . First I
Just to get it straight in my head. Consider this example bit of Erlang

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.