0

I have declared global variable named counter=0 on my index.html page inside head section

<script>
var counter=0;
</script>

now in my one of function i am setting its value as

    function getCounter(param)
    {
        $.getJSON("somewebserviceURL&format=json&callback=?",
            function(data)
            {
                 $.each(data, function(i, item)
                    {
                        counter++;
                    });

            });
//I am not able to get the latest value here & in upcoming functions which use this variable
         alert(counter);

    }
1

6 Answers 6

3

This is because getJSON is asynchronous. This means the counter variable will not have been incremented before alert(counter) is hit. Instead, move the alert to just after your $.each() loop:

function getCounter(param) {
    $.getJSON(
        "somewebserviceURL&format=json&callback=?",
        function(data) {
            $.each(data, function(i, item) {
                counter++;
            });
            alert(counter);
        }
    );
}
Sign up to request clarification or add additional context in comments.

3 Comments

can you tell me the way that i can get count value outside of $.getJSON() call or if i use $.POST() method from jquery does it resolve problem?
You can only work with the count value inside the callback function of an AJAX method, whether you do this directly, as in your example, or by calling a function elsewhere and passing your variables to it.
basically i want $.getJSON() should executed its success function & code should run according to line numbers means top to bottom without skipping any line
1

That is because getJSON is sent asynchronously. Your alert is called before the success-callback of getJSON. The success-callback isn't called until you have the response, by then alert() has already fired.

Comments

1

Simply because your alert() is processed faster than your counter++;

Your .getJSON() is simply an AJAX-Call which is asynchronous.

Which means the JavaScript code, does not wait until your AJAX Call is finished, it continues without waiting.

Comments

0
function getCounter(param)
{
    $.getJSON("somewebserviceURL&format=json&callback=?",
        function(data)
        {
             $.each(data, function(i, item)
                {
                    counter++;
                });
             // try this
             alert(counter);
        }
     );
}

Comments

0

You could probably use a callback for this :

function getCounter(param, callback) {
    $.getJSON("somewebserviceURL&format=json&callback=?",
        function(data)
        {
             $.each(data, function(i, item)
                {
                    counter++;
                });

             callback.call(this, counter);
        }
     );
}

getCounter(param, function(counter) { alert(counter);  } );

Comments

0

All of are true here that GetJson is asynchronous!

Just shift alert() to inside fore each loop to see affected value as per below

 function getCounter(param)
    {
        $.getJSON("somewebserviceURL&format=json&callback=?",
            function(data)
            {
                 $.each(data, function(i, item)
                    {
                        counter++;
                        alert(counter);

                    });

            });

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.