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

The Archive Base Latest Questions

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

javascript snippet : <script type=text/javascript> $(document).ready(function() { setInterval(function() { $.getJSON(‘notif.php’, function(data) { my_var =

  • 0

javascript snippet :

<script type="text/javascript">
$(document).ready(function() {

    setInterval(function() {
        $.getJSON('notif.php', function(data) {
            my_var = '123';
        });
        alert("my var =" + my_var);
    }, 2000);

});    
</script>

in chrome and FF console, my_var shows to be undefined, but its value is shown on the alertbox. if undefined , then how is its val

What is the explanation ?

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

    getJSON in jquery is by default asynch call so the alert("my var =" + my_var); will be executed without waiting for the response of the ajax call.

    move the alert inside callback function like

    setInterval(function() {
        $.getJSON('notif.php', function(data) {
            my_var = '123';
            alert("my var =" + my_var);
        });
    }, 2000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this: <script type=text/javascript> jQuery(document).ready(function(){ jQuery('#menu-main-menu li a').after('<p class=separator>/</p>'); jQuery(.textwidget a[href^='http://']).attr(target,_blank); }); </script>
I have index.php like this: <script type=text/javascript src=jquery-1.4.2.js></script> <script type=text/javascript src=ajax.js></script> <a href='one.php' class='ajax'>One</a>
<noscript><div id=example>I want to get this innerHTML</div></noscript> <script type=text/javascript> alert($('example').innerHTML);</script> This javascript snippet just
I have this Javascript snippet in my application to prevent clickjacking: <script language=javascript type=text/javascript>
I have this snippet: <script type=text/javascript> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-11111111-1']);
I have the following js snippet: <script language=javascript> function test() { this.a = 10;
I have a PHP-script displaying random text: <?php $quotes = array( 'quote 1', 'quote
I have a JavaScript snippet that runs very well on Firefox and Safari, but
Does anyone have a library or JavaScript snippet to validate the check digit of
I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.

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.