Friday, 15 March 2013

javascript - Variable scope in jQuery AJAX requests? -



javascript - Variable scope in jQuery AJAX requests? -

okay, i'm totally confused. i'm sure scoping or closure thing, explain why

$(document).ready(function() { var test = 0; var whatev = [1,2,3,4,5]; $.each(whatev, function(i, v) { test += 1; }); alert(test); });

alerts "5" and

$(document).ready(function() { var test = 0; var whatev = [1,2,3,4,5]; $.each(whatev, function(i, v) { $.ajax('http://example.com/').done(function(data) { test += 1; }); }); alert(test); });

alerts 0? why anonymous function in $.each phone call have access test anonymous function ajax phone call doesn't? anonymous function beingness defined?

the callback of jquery each synchronous.

the callback of jquery ajax (asynchronous javascript , xml) asynchronous.

if want ajax function synchronous can such:

$.ajax({ type: "get", url: "http://example.com/", async: false })

javascript jquery ajax

No comments:

Post a Comment