<?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"
	>
<channel>
	<title>Comments on: setInterval() Scope problem &#038; solutions for Firefox and Internet Explorer 6</title>
	<atom:link href="http://alexle.net/archives/169/feed" rel="self" type="application/rss+xml" />
	<link>http://alexle.net/archives/169</link>
	<description>Personal View</description>
	<pubDate>Sat, 22 Nov 2008 04:45:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Mike</title>
		<link>http://alexle.net/archives/169#comment-225820</link>
		<dc:creator>Mike</dc:creator>
		<pubDate>Wed, 29 Oct 2008 18:51:26 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-225820</guid>
		<description>Thanks for all this info!  This helped me a lot, but I am still having problems.  How does clearInterval fit into all this.  Using Tim's code about javascript closures, I was able to get the setInterval working, but it would not end!  I would call clearInterval(this.timer) (this.timer being the return from setInterval, of course) from a different function, and it would not work.  How can you hook the timer variable into the scope so that it will cause the interval to stop running once it is cleared?</description>
		<content:encoded><![CDATA[<p>Thanks for all this info!  This helped me a lot, but I am still having problems.  How does clearInterval fit into all this.  Using Tim&#8217;s code about javascript closures, I was able to get the setInterval working, but it would not end!  I would call clearInterval(this.timer) (this.timer being the return from setInterval, of course) from a different function, and it would not work.  How can you hook the timer variable into the scope so that it will cause the interval to stop running once it is cleared?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vik</title>
		<link>http://alexle.net/archives/169#comment-197995</link>
		<dc:creator>Vik</dc:creator>
		<pubDate>Thu, 14 Aug 2008 03:35:08 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-197995</guid>
		<description>Thank you very much - my initial solution to the problem was a global array of objects - but using closure is much cleaner =)</description>
		<content:encoded><![CDATA[<p>Thank you very much - my initial solution to the problem was a global array of objects - but using closure is much cleaner =)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: setInterval DIV bewegen IE Problem - jswelt - Forum (Javascript, PHP, MySQL, AJAX, Webdesign)</title>
		<link>http://alexle.net/archives/169#comment-169951</link>
		<dc:creator>setInterval DIV bewegen IE Problem - jswelt - Forum (Javascript, PHP, MySQL, AJAX, Webdesign)</dc:creator>
		<pubDate>Mon, 16 Jun 2008 18:04:57 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-169951</guid>
		<description>[...] -    Heute, 20:04         Tach,  Google-Suchstring &#34;ie setinterval problem&#34;, Platz 1:  Alex Le’s Blog » Blog Archive » setInterval() Scope problem &#38; solutions for Firefox and Internet...     Gruß, [...]</description>
		<content:encoded><![CDATA[<p>[&#8230;] -    Heute, 20:04         Tach,  Google-Suchstring &quot;ie setinterval problem&quot;, Platz 1:  Alex Le’s Blog » Blog Archive » setInterval() Scope problem &amp; solutions for Firefox and Internet&#8230;     Gruß, [&#8230;]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sepehr Lajevardi</title>
		<link>http://alexle.net/archives/169#comment-164171</link>
		<dc:creator>Sepehr Lajevardi</dc:creator>
		<pubDate>Tue, 03 Jun 2008 21:07:38 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-164171</guid>
		<description>Many thanks Alex. 
I was absolutely disappointed about IE.</description>
		<content:encoded><![CDATA[<p>Many thanks Alex.<br />
I was absolutely disappointed about IE.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Valentiate</title>
		<link>http://alexle.net/archives/169#comment-134437</link>
		<dc:creator>David Valentiate</dc:creator>
		<pubDate>Wed, 26 Mar 2008 15:17:30 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-134437</guid>
		<description>After hitting my head against a similar wall and coming to a similar conclusion I couldn't accept it. I'm glad I didn't.

base on Tim Cooijmans solution I wrote a little state machine function. The timing code on it boils down to this.

this.myFunction = function() {
  var delay = 100;
  var obj = this;
  setTimeout(function() {obj.myFunction()}, delay);
}</description>
		<content:encoded><![CDATA[<p>After hitting my head against a similar wall and coming to a similar conclusion I couldn&#8217;t accept it. I&#8217;m glad I didn&#8217;t.</p>
<p>base on Tim Cooijmans solution I wrote a little state machine function. The timing code on it boils down to this.</p>
<p>this.myFunction = function() {<br />
  var delay = 100;<br />
  var obj = this;<br />
  setTimeout(function() {obj.myFunction()}, delay);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron Michaux</title>
		<link>http://alexle.net/archives/169#comment-111750</link>
		<dc:creator>Aaron Michaux</dc:creator>
		<pubDate>Tue, 05 Feb 2008 13:09:33 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-111750</guid>
		<description>Excellent description of the problem. I found the simplest solution uses the closure feature of the language as follows:

var foo = this;
setInterval(function() { foo.bar(); }, 900);

Closure means that the local variable 'foo' will persist because it is referenced by that anonymous... function() {foo.bar();}</description>
		<content:encoded><![CDATA[<p>Excellent description of the problem. I found the simplest solution uses the closure feature of the language as follows:</p>
<p>var foo = this;<br />
setInterval(function() { foo.bar(); }, 900);</p>
<p>Closure means that the local variable &#8216;foo&#8217; will persist because it is referenced by that anonymous&#8230; function() {foo.bar();}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Almir</title>
		<link>http://alexle.net/archives/169#comment-87278</link>
		<dc:creator>Almir</dc:creator>
		<pubDate>Tue, 11 Dec 2007 15:15:54 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-87278</guid>
		<description>Hi Alex

I really appreciate what you have shown us here. Great work!</description>
		<content:encoded><![CDATA[<p>Hi Alex</p>
<p>I really appreciate what you have shown us here. Great work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Francesco MunafÃ²</title>
		<link>http://alexle.net/archives/169#comment-67166</link>
		<dc:creator>Francesco MunafÃ²</dc:creator>
		<pubDate>Thu, 18 Oct 2007 14:29:55 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-67166</guid>
		<description>If you want, using a "pointer" (use an array in AS), you can pass the interval id, as a parameter, to the callback function thus solving any scope problem.

This way:

function DelayedTrace(txt) {
	var Pointer=[];
	var interval=setInterval(function(Ptr,txt) { clearInterval(Ptr[0]); trace("OK") },2000,Pointer,txt);
	Pointer[0]=interval;
}


[ this function will write a trace of the parameter after 3 seconds and then clean up after herself, cleaning the interval no matter the scope]
DelayedTrace("Hello");</description>
		<content:encoded><![CDATA[<p>If you want, using a &#8220;pointer&#8221; (use an array in AS), you can pass the interval id, as a parameter, to the callback function thus solving any scope problem.</p>
<p>This way:</p>
<p>function DelayedTrace(txt) {<br />
	var Pointer=[];<br />
	var interval=setInterval(function(Ptr,txt) { clearInterval(Ptr[0]); trace(&#8221;OK&#8221;) },2000,Pointer,txt);<br />
	Pointer[0]=interval;<br />
}</p>
<p>[ this function will write a trace of the parameter after 3 seconds and then clean up after herself, cleaning the interval no matter the scope]<br />
DelayedTrace(&#8221;Hello&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David B</title>
		<link>http://alexle.net/archives/169#comment-48169</link>
		<dc:creator>David B</dc:creator>
		<pubDate>Fri, 03 Aug 2007 02:07:22 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-48169</guid>
		<description>Good Article, great comments guys.  Thanks for the insight, I've been at odds end with calling my object methods inside setTimeout!</description>
		<content:encoded><![CDATA[<p>Good Article, great comments guys.  Thanks for the insight, I&#8217;ve been at odds end with calling my object methods inside setTimeout!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Frenkiel</title>
		<link>http://alexle.net/archives/169#comment-42420</link>
		<dc:creator>David Frenkiel</dc:creator>
		<pubDate>Tue, 03 Jul 2007 15:36:48 +0000</pubDate>
		<guid isPermaLink="false">http://alexle.net/archives/169#comment-42420</guid>
		<description>Hi Alex and all,

The solutions described here are nice but they don't take full advantage of the mechanisms provided by the language.
Look up Function.apply(). This is a much cleaner way than using eval(). Extending Ben's approach of using a closure, you can do the following:

function delayMenu(element) {
var oThis = this; 
// assuming this code is executed in the scope of some object that you wish to preserve in the showMenu callback.

var e = element;
setTimeout(function() { oThis.showMenu.apply(oThis, [e]); }, 1000);
}

More info: http://msdn2.microsoft.com/en-us/library/4zc42wh1.aspx</description>
		<content:encoded><![CDATA[<p>Hi Alex and all,</p>
<p>The solutions described here are nice but they don&#8217;t take full advantage of the mechanisms provided by the language.<br />
Look up Function.apply(). This is a much cleaner way than using eval(). Extending Ben&#8217;s approach of using a closure, you can do the following:</p>
<p>function delayMenu(element) {<br />
var oThis = this;<br />
// assuming this code is executed in the scope of some object that you wish to preserve in the showMenu callback.</p>
<p>var e = element;<br />
setTimeout(function() { oThis.showMenu.apply(oThis, [e]); }, 1000);<br />
}</p>
<p>More info: <a href="http://msdn2.microsoft.com/en-us/library/4zc42wh1.aspx" rel="nofollow">http://msdn2.microsoft.com/en-us/library/4zc42wh1.aspx</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.251 seconds -->
