<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Passing arguments to javascript&#8217;s setTimeout() method using closures</title>
	<atom:link href="http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/</link>
	<description></description>
	<lastBuildDate>Tue, 29 Dec 2009 15:24:27 -0800</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Trev</title>
		<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/comment-page-1/#comment-1600</link>
		<dc:creator>Trev</dc:creator>
		<pubDate>Tue, 01 Dec 2009 12:32:32 +0000</pubDate>
		<guid isPermaLink="false">http://lejnieks.com/?p=30#comment-1600</guid>
		<description>Another version that seems to work is simply: 

&lt;code&gt;setTimeout(&quot;doAlert(&quot;+msg+&quot;)&quot;, 1);&lt;/code&gt;

Might get a bit confusing with multiple arguments mind you, but works in IE8, FF3 and Chrome (so presumably also in Safari). Not got IE7 to test it on however.</description>
		<content:encoded><![CDATA[<p>Another version that seems to work is simply: </p>
<p><code>setTimeout("doAlert("+msg+")", 1);</code></p>
<p>Might get a bit confusing with multiple arguments mind you, but works in IE8, FF3 and Chrome (so presumably also in Safari). Not got IE7 to test it on however.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: archerid</title>
		<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/comment-page-1/#comment-1584</link>
		<dc:creator>archerid</dc:creator>
		<pubDate>Thu, 27 Aug 2009 20:40:52 +0000</pubDate>
		<guid isPermaLink="false">http://lejnieks.com/?p=30#comment-1584</guid>
		<description>This was incredibly helpful. I&#039;ve spent an embarrassingly long time trying to track down the right way to pass params through setTimeout. Thanks for posting your method.</description>
		<content:encoded><![CDATA[<p>This was incredibly helpful. I&#8217;ve spent an embarrassingly long time trying to track down the right way to pass params through setTimeout. Thanks for posting your method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/comment-page-1/#comment-231</link>
		<dc:creator>John</dc:creator>
		<pubDate>Fri, 06 Feb 2009 17:25:33 +0000</pubDate>
		<guid isPermaLink="false">http://lejnieks.com/?p=30#comment-231</guid>
		<description>Follow-up:

Well, it seems as though you&#039;re right and wrong Thomas. Firefox does subscribe to what appears to be the standard

https://developer.mozilla.org/En/DOM/Window.setTimeout

but IE, of course does not. Thomas&#039; syntax doesn&#039;t work on IE7 (I tried it).

An alternative--but less elegant--solution that works in both browsers is to frame the called function/parameters in quotes, thus:

function called(a,b) {
       alert(a+&quot;-&quot;+b)
       }
function caller(a,b) {
       setTimeout(&#039;called(&#039;+a+&#039;,&#039;+b+&#039;)&#039;,2000);
       }

     Later code ....
      caller(1,2);caller(3,4)
      Will show alerts of &quot;1-2&quot; and &quot;3-4&quot; after 2 seconds.</description>
		<content:encoded><![CDATA[<p>Follow-up:</p>
<p>Well, it seems as though you&#8217;re right and wrong Thomas. Firefox does subscribe to what appears to be the standard</p>
<p><a href="https://developer.mozilla.org/En/DOM/Window.setTimeout" rel="nofollow">https://developer.mozilla.org/En/DOM/Window.setTimeout</a></p>
<p>but IE, of course does not. Thomas&#8217; syntax doesn&#8217;t work on IE7 (I tried it).</p>
<p>An alternative&#8211;but less elegant&#8211;solution that works in both browsers is to frame the called function/parameters in quotes, thus:</p>
<p>function called(a,b) {<br />
       alert(a+&#8221;-&#8221;+b)<br />
       }<br />
function caller(a,b) {<br />
       setTimeout(&#8216;called(&#8216;+a+&#8217;,'+b+&#8217;)',2000);<br />
       }</p>
<p>     Later code &#8230;.<br />
      caller(1,2);caller(3,4)<br />
      Will show alerts of &#8220;1-2&#8243; and &#8220;3-4&#8243; after 2 seconds.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/comment-page-1/#comment-230</link>
		<dc:creator>John</dc:creator>
		<pubDate>Fri, 06 Feb 2009 07:47:50 +0000</pubDate>
		<guid isPermaLink="false">http://lejnieks.com/?p=30#comment-230</guid>
		<description>Good comment by Thomas and while I accept your authority on this you&#039;re about the only person out there defining the call this way. Most  of the examples I&#039;ve seen pass a &quot;call to the function&quot; and do so in quotes! ie:

     setTimeout(&#039;someFunction()&#039;,etc)

And one reference (I can&#039;t remember where) defines that third parameter as an optional definition of the language of the called function!

Your solution works, of course.</description>
		<content:encoded><![CDATA[<p>Good comment by Thomas and while I accept your authority on this you&#8217;re about the only person out there defining the call this way. Most  of the examples I&#8217;ve seen pass a &#8220;call to the function&#8221; and do so in quotes! ie:</p>
<p>     setTimeout(&#8216;someFunction()&#8217;,etc)</p>
<p>And one reference (I can&#8217;t remember where) defines that third parameter as an optional definition of the language of the called function!</p>
<p>Your solution works, of course.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thomas</title>
		<link>http://www.lejnieks.com/2008/08/21/passing-arguments-to-javascripts-settimeout-method-using-closures/comment-page-1/#comment-229</link>
		<dc:creator>Thomas</dc:creator>
		<pubDate>Mon, 15 Dec 2008 20:11:55 +0000</pubDate>
		<guid isPermaLink="false">http://lejnieks.com/?p=30#comment-229</guid>
		<description>There&#039;s an easier way to do this:

&lt;code&gt;
...
var msg = &quot;Hello World&quot;;

setTimeout(doAlert, 1, [msg]);
...

function doAlert(msg)
{
	alert(msg);
}
&lt;/code&gt;

There&#039;s no need for the delay function.

Furthermore, when passing a reference to a function as the first parameter to the setTimeout method, you&#039;re supposed to pass the &lt;strong&gt;reference&lt;/strong&gt; to the function, not a &lt;strong&gt;call&lt;/strong&gt; to the function.

e.g.
&lt;code&gt;
// passing a call to the function is incorrect
var t = setTimeout(someFunction(), 1, argsArray);
&lt;/code&gt;

vs.

&lt;code&gt;
// passing a *reference* to the function is correct
var t = setTimeout(someFunction, 1, argsArray);
&lt;/code&gt;

Enjoy,

Thomas</description>
		<content:encoded><![CDATA[<p>There&#8217;s an easier way to do this:</p>
<p><code><br />
...<br />
var msg = "Hello World";</p>
<p>setTimeout(doAlert, 1, [msg]);<br />
...</p>
<p>function doAlert(msg)<br />
{<br />
	alert(msg);<br />
}<br />
</code></p>
<p>There&#8217;s no need for the delay function.</p>
<p>Furthermore, when passing a reference to a function as the first parameter to the setTimeout method, you&#8217;re supposed to pass the <strong>reference</strong> to the function, not a <strong>call</strong> to the function.</p>
<p>e.g.<br />
<code><br />
// passing a call to the function is incorrect<br />
var t = setTimeout(someFunction(), 1, argsArray);<br />
</code></p>
<p>vs.</p>
<p><code><br />
// passing a *reference* to the function is correct<br />
var t = setTimeout(someFunction, 1, argsArray);<br />
</code></p>
<p>Enjoy,</p>
<p>Thomas</p>
]]></content:encoded>
	</item>
</channel>
</rss>
