<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>loop &#8211; Blackbams Blog</title>
	<atom:link href="https://blog.blackbam.at/tag/loop/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.blackbam.at</link>
	<description>development - digital arts - internet</description>
	<lastBuildDate>Tue, 08 Dec 2015 17:30:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8</generator>
	<item>
		<title>Ajax (jQuery) and loops: Local variable scoping and callback functions</title>
		<link>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/</link>
					<comments>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/#respond</comments>
		
		<dc:creator><![CDATA[Blackbam]]></dc:creator>
		<pubDate>Tue, 08 Dec 2015 17:30:15 +0000</pubDate>
				<category><![CDATA[JavaScript / Ajax]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[callback]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[scoping]]></category>
		<category><![CDATA[variable]]></category>
		<guid isPermaLink="false">https://blog.blackbam.at/?p=2273</guid>

					<description><![CDATA[Thats a very annoying common problem I discovered &#8211; if calling Ajax within a Javascript loop the callback function will be called asynchronously. That means: All local variables in the callback function will have their future value instead the value they had when the loop was running. &#160; Example 1 (probably unwanted results): var types [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Thats a very annoying common problem I discovered &#8211; if calling Ajax within a Javascript loop the callback function will be called asynchronously. That means: All local variables in the callback function will have their future value instead the value they had when the loop was running.</p>
<p>&nbsp;</p>
<p><span style="color: #ff6666;">Example 1 (probably unwanted results):</span></p>
<pre lang="javascript">var types = ["boring","stupid","funny"];

for(key in types) {
    var type=types[key];

    $.ajax({url: "/?connection=get&amp;type="+type, success: function(result){
        $('#'+type).html(result);
    }
    });
}

</pre>
<p>Result: Only #funny is populated (with the results of the last callback, no matter which ends last).</p>
<p>&nbsp;</p>
<p><span style="color: #66ff66;">Example 2 (solution):</span></p>
<pre lang="javascript">
var types = ["boring","stupid","funny"];

for(key in types) {
    var type=types[key];

    (function(type) {
        $.ajax({url: "/?connection=get&amp;type="+type, success: function(result){
            $('#'+type).html(result);
        }
        });
    })(type);
}

</pre>
<p>Result: The elements with ids #boring, #stupid and #funny are populated with the result of the corresponding callback.</p>
<p>&nbsp;</p>
<p>Explanation: You have created a closure which is preserving the value of type according to the current iteration.</p>
<p><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fblog.blackbam.at%2F2015%2F12%2F08%2Fajax-jquery-and-loops-local-variable-scoping-and-callback-functions%2F&#038;title=Ajax%20%28jQuery%29%20and%20loops%3A%20Local%20variable%20scoping%20and%20callback%20functions" data-a2a-url="https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/" data-a2a-title="Ajax (jQuery) and loops: Local variable scoping and callback functions"><img src="https://static.addtoany.com/buttons/share_save_120_16.png" alt="Share"></a></p>]]></content:encoded>
					
					<wfw:commentRss>https://blog.blackbam.at/2015/12/08/ajax-jquery-and-loops-local-variable-scoping-and-callback-functions/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
