[Greasemonkey] setTimeout() problems

A. Alfred Ayache alfred at lastbyte.ca
Wed May 24 13:35:16 EDT 2006


Try this:

   var arr = new Array("foo", "bar", "baz");
   var i = 0;
   function tLoop() {
     alert(arr[i]);
     if (++i < arr.length) {
       window.setTimeout(tLoop, 1000);
     }
   }

The way you've got it set up below seems to make the timeouts step on each 
other, no?

A. Alfred Ayache
http://lphs76.ca             - Reunion community
http://www.rentersPlus.com   - Apartment Search
http://www.lastbyte.ca       - Web Design, eCommerce, PHP/MySQL, Java, Oracle


Premshree Pillai wrote:
> On 5/24/06, Jonathan Buchanan <jonathan.buchanan at gmail.com> wrote:
>> function(arg) { return function() { alert(arg); }; }(foo);
> 
> This seems to work fine. Only the timeout itself doesn't seem to be
> working fine. Let me be more specific: I'm calling the setTimeout()
> within a loop, like thus:
> 
>   var arr = new Array("foo", "bar", "baz");
>   for (var i=0; i<arr.length; i++) {
>      window.setTimeout(
>         function(arg) { return function() { alert(arg); }; }(arr[i]),
>         1000
>      );
>   }
> 
> The above only alerts "foo" and then does nothing. OTOH, with a
> smaller timout (300), it runs fine (well, almost) -- the loop executes
> twice. :-/
> 
> Thoughts?
> 
> Premshree
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkey at mozdev.org
> http://mozdev.org/mailman/listinfo/greasemonkey
> 
> --------------------------------
> Spam/Virus scanning by CanIt Pro
> 
> For more information see
> http://www.kgbinternet.com/SpamFilter.htm
> 
> To control your spam filter, log in at
> http://filter.kgbinternet.com
> 
> 


More information about the Greasemonkey mailing list