SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to update globale variable from within a callback function?

    Hello,

    I have a problem where I am not able to update a global variable from inside a callback function.

    Here is my code:

    Code JavaScript:
    // Set global variable.
    var load_values_from_server = "initial value";
     
    // Call our page that shows server load and put it into "results" variable.
    microAjax("[URL]http://domain.com/show-server-load.php[/URL]?" + randomnumber, 
     function (results) { 
        load_values_from_server = results; 
     });
     
    // We want this to show the "results" from the callback function.
    alert(load_values_from_server);

    The problem is that the alert always shows "initial value" and never the "results" from the callback function.

    What am I missing? Shouldn't global variables be global, even within a callback function?

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by hstraf View Post
    I have a problem where I am not able to update a global variable from inside a callback function.
    It seems that you may have a fundamental misunderstanding when it comes to Ajax.

    Ajax calls are set aside for some later time. The script carries on executing its code, and later on after Ajax has requested its content and received a response, only then (which is a long time after the rest of the script has finished) does the associated Ajax function get run.

    So if you want to do something with the result in the callback function, it is inside of that callback function that you should do it, which can include calling other functions that you need too.


    Source: http://fixingthesejquery.com/#slide40
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! That makes perfect sense and I was able to figure out how to make this work.

    Nice and simple. Here is the new code:

    Code JavaScript:
    // Set global variable.
    var load_values_from_server = "initial value";
     
    // Call our page that shows server load and put it into "results" variable.
    microAjax("[URL]http://domain.com/show-server-load.php[/URL]?" + randomnumber, 
     function (results) { 
        show_results(results);
     });
     
    function show_results(results) {
     // We want this to show the "results" from the callback function.
     alert(load_values_from_server);
    }
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •