List Info

Thread: setTimeout() problems




setTimeout() problems
user name
2006-05-24 10:15:57
Hi group,

I'm using a setTimeout within a userscript. The function
that I need
to pass as argument accepts an argument. Sample code:

   var foo = "bleh";
   window.setTimeout(function(arg) { alert(arg); }(foo),
300);

I get an error--"useless setTimeout call (missing
quotes around
argument?)"--on execution.

Any thoughts?

Premshree
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:18:16
On 5/24/06, Premshree Pillai <premshree.pillaigmail.com> wrote:
>    var foo = "bleh";
>    window.setTimeout(function(arg) { alert(arg);
}(foo), 300);

That's the same as this (modulo the actual alert at the
time
setTimeout is called):
window.setTimeout(void, 300);

I think you mean this:

window.setTimeout(function() {
  function(arg) { alert(arg); }(foo)
}, 300);

or perhaps this:

window.setTimeout(function() {
  alert(foo)
}, 300);
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:04:52
setTimeout needs a function to execute - you're immediately
calling
the function you just defined, so as you're not returning
anything in
that function, you're really passing undefined to
setTimeout.

Try returning a function which forms a closure over the
argument.

function(arg) { return function() { alert(arg); }; }(foo);

Jonathan.

On 5/24/06, Premshree Pillai <premshree.pillaigmail.com> wrote:
> Hi group,
>
> I'm using a setTimeout within a userscript. The
function that I need
> to pass as argument accepts an argument. Sample code:
>
>   var foo = "bleh";
>   window.setTimeout(function(arg) { alert(arg); }(foo),
300);
>
> I get an error--"useless setTimeout call (missing
quotes around
> argument?)"--on execution.
>
> Any thoughts?
>
> Premshree
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkeymozdev.org
> http:
//mozdev.org/mailman/listinfo/greasemonkey
>
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:28:53
Hi Premshree:

I found a slideshow on DiveIntoGreasemonkey.org which
explains how to do this:

http://divein
togreasemonkey.org/slides/

It's on slide 11, which you can get to by hovering in the
lower right and using 
the drop-down.

 >>>
Can’t auto-eval chunks of JavaScript code in strings

Fails:

window.setTimeout("my_func()", 1000);

Works:

window.setTimeout(my_func, 1000);
<<<


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:
> Hi group,
> 
> I'm using a setTimeout within a userscript. The
function that I need
> to pass as argument accepts an argument. Sample code:
> 
>   var foo = "bleh";
>   window.setTimeout(function(arg) { alert(arg); }(foo),
300);
> 
> I get an error--"useless setTimeout call (missing
quotes around
> argument?)"--on execution.
> 
> Any thoughts?
> 
> Premshree
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkeymozdev.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.kgbintern
et.com
> 
> 
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:22:41
Code in SetTimeout needs to be in speech quotes, I think.
The code is 
sent as a string and evaluated by setTimeout
setTimeout("runme()", 300);

If you need to pass a value from your script into the code
in 
setTimeout, then you need something like this:
var foo
setTimeout("runme("+foo+")", 300);

I think. 


If you are passing values to it from your script

Premshree Pillai wrote:
> Hi group,
>
> I'm using a setTimeout within a userscript. The
function that I need
> to pass as argument accepts an argument. Sample code:
>
>   var foo = "bleh";
>   window.setTimeout(function(arg) { alert(arg); }(foo),
300);
>
> I get an error--"useless setTimeout call (missing
quotes around
> argument?)"--on execution.
>
> Any thoughts?
>
> Premshree
> _______________________________________________
> Greasemonkey mailing list
> Greasemonkeymozdev.org
> http:
//mozdev.org/mailman/listinfo/greasemonkey
>
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:22:50
On ons, 2006-05-24 at 15:45 +0530, Premshree Pillai wrote:
> Hi group,
> 
> I'm using a setTimeout within a userscript. The
function that I need
> to pass as argument accepts an argument. Sample code:
> 
>    var foo = "bleh";
>    window.setTimeout(function(arg) { alert(arg);
}(foo), 300);
> I get an error--"useless setTimeout call (missing
quotes around
> argument?)"--on execution.
> 
> Any thoughts?

The function call to the anonymous function with the
argument foo occurs
when you call window.setTimeOut, and the anonymous function
returns
null. So what you pass to window.setTimeOut is null and 300.
The first argument should probably be a function that takes
no
arguments. I'm not really shure what you're after,
if the idea is to preserve the value of foo to the timeout,
just do like
this:

window.setTimeout(function () {alert(foo); },300); 

foo in the anoymous function will refer to the foo in the
scope when
window.setTimeOut was called. 

Or if you want to pass a function that takes one parameter,
make another
anonymous function round the whole thing:

window.setTimeout(function () {
function (arg) {alert(arg); }(foo);
},300); 

Hope it helps,
/Henrik 
 
------------------------------------
Try it at http://stix.to
Read the blog http://blog.stix.to
Send "subscribe" to newsstix.to
------------------------------------

_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 13:35:10
On 5/24/2006 6:15 AM, Premshree Pillai wrote:
> I'm using a setTimeout within a userscript. The
function that I need
> to pass as argument accepts an argument. Sample code:

I have only seen examples, not documentation, so I can't
explain why. 
But I know this works:

function foo(arg) {
	alert(arg);
}
setTimeout(foo, 10, 'bar');
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 15:24:01
On 5/24/06, Vectorspace <vectorspacentlworld.com> wrote:
> setTimeout("runme("+foo+")",
300);

That's how I would've done it in JavaScript normally. In
GM you need
to pass the callback as an object instead of a string.

Premshree
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 15:20:53
On 5/24/06, Jonathan Buchanan <jonathan.buchanangmail.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
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
setTimeout() problems
user name
2006-05-24 16:35:16
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.buchanangmail.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
> Greasemonkeymozdev.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.kgbintern
et.com
> 
> 
_______________________________________________
Greasemonkey mailing list
Greasemonkeymozdev.org
http:
//mozdev.org/mailman/listinfo/greasemonkey
[1-10] [11-12]

about | contact  Other archives ( Real Estate discussion Medical topics )