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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:13:36+00:00 2026-06-01T21:13:36+00:00

Here is the code :- var quiz; function startQuiz() { $.ajax({ url: ‘get_quiz/’, cache:

  • 0

Here is the code :-

var quiz;
function startQuiz() {

     $.ajax({
        url: 'get_quiz/',
        cache: 'false',
        dataType: 'json',
        async: 'false',
        success: function(data) {
            quiz = data;
            alert(quiz[0].q);   // I'm able to access quiz here     

},   
    }    
);  
    }

startQuiz();

alert(quiz[0].q);   // Not able to access it here.

I’m not able to access quiz here, am I mission something?, Whats wrong with 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-01T21:13:37+00:00Added an answer on June 1, 2026 at 9:13 pm

    Ajax is assynchronous which can be an unfamiliar concept. Your code will run like this:

    1. var quiz;
    2. define function startQuiz;
    3. call startQuiz;
    4. do ajax call (and continue! don't block)
    5. alert(quiz[0].q);    // Not able to access it here.
    -- ajax call comes back
    6. quiz = data;
    7. alert(quiz[0].q);       // I'm able to access quiz here
    

    Ajax is asynchronous, it doesn’t block. This means that when you make the ajax call the callback doesn’t actually get called until the ajax call returns, it doesn’t block and wait. Instead the code will continue on.

    Then later when the ajax call returns the data, your callback function will be executed.

    Javascript does this by means of an event loop.

    See it like this: steps 1-5 are part of the first event. 6-7 are part of the second event.

    A cool thing about JavaScript is that in your callback you still have access to anything above it (like the variable quiz) because of scoping. This is called a closure. Your callback function closes around the scope and brings it with him to the next event.

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

Sidebar

Related Questions

Here is my code: var showNo = 1; window.setInterval(function() { console.log(showNo); if(showNo === 1)
Here is some code: var class = function(elem,div){ this.elem= elem; this.div = div; this.init
I have this code here: var Person = (function() { var name; var PersonConstructor
Here is my chunk of code: var update_shipping_methods = function(methods) { $(methods).each( function(i) {
Here is my Ajax code: var myJSONObject = {bindings: [ {ircEvent: PRIVMSG, method: newURI,
Here is my code: var var_ids = new Array(); var i = 0; jQuery(select).each(function()
First off, here's my code: var variable1 = 10; function f1 (){ alert(variable1); }
First of all, here's my code: var users = {}; //User Connect io.sockets.on('connection', function
I have the following code: var myModule = (function($) { // Insert code here
In my code here: var manTimer:Timer = new Timer(1700,5); manTimer.addEventListener(TimerEvent.TIMER, moveMan); function moveMan(e:TimerEvent):void {

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.