Thursday 15 January 2015

javascript - Why does moving the console.log() for a variable into a second AJAX call change the output? -


I'm not completely sure that when I move console.log () to another AJAX call, my code Why does the output change?

Three consoles between two AJAX calls. By dividing the logs, this code deserves me (all productTitles with their productURLs and numProductTopics):

But all three consoles After moving the logs to another AJAX call, this code gives the same productual and product URL with each of the different, expected numProductTopics:

Anyone please Can explain?

That's because you have a loop where you value your variable and call AJAX Let's start, which will be executed before your AJAX success callback is executed. Therefore they will hold the value of that final repetition. if (object canonical_name & object.name) {var productTitle = object.name; ProductURL = "Failure / Trenet / Products /" + Object. Canonical_name; JQuery.ajax ({type: 'GET', url: apiBaseURL + 'topics.json? Product =' + object.canonical_name, data type: 'jsonp', success: function (result 2) {var numProductTopics = results2.total; Console log (ProductTital); // & lt; - The last one will be the console .log (productURL); // & lt; - the last run in this will be value from console.log (numProductTopics);}};

You can also solve it by adding the variable for each function. For each variation, locker wants to lock the voxy as variables.



No comments:

Post a Comment