Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to write something like setTimeout

Reply
Thread Tools

how to write something like setTimeout

 
 
Antony Sequeira
Guest
Posts: n/a
 
      05-20-2004
Hi
While looking at some code I realized that the built in
setTimeout
function takes a string that is later
evaluated in the original caller's context.
How does one achieve something similar in user defined functions.

-Antony

 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      05-20-2004
Antony Sequeira said:
>
>Hi
>While looking at some code I realized that the built in
>setTimeout
>function takes a string that is later
>evaluated in the original caller's context.
>How does one achieve something similar in user defined functions.


You use setTimeout.
In other words, your question isn't clear.
Why won't setTimeout do what you want?

 
Reply With Quote
 
 
 
 
Yann-Erwan Perio
Guest
Posts: n/a
 
      05-20-2004
Antony Sequeira wrote:

> While looking at some code I realized that the built in
> setTimeout
> function takes a string that is later
> evaluated in the original caller's context.


This original context is always the global object (window). However, you
can perfectly use a function reference or a function expression instead
of a string as the first argument for setTimeout - this isn't supported
by old browsers though, which would then call the toString() method for
the function while evaluating the setTimeout first argument - test
accordingly.

function foo(){
var bar="hello, world!";
setTimeout(
function(){
alert(bar);
},
1000
);
}

Using a function expression is much more powerful than using a string
argument, since you can control the scope chain and add a specific scope
object, just for this function (either using an outer function or a
"with" statement).

---
setTimeout(
(function(){
var foo=0, bar=0;
return function(){
window.status="foo:"+foo+", bar:"+bar;
if(foo++<1000 && bar-->-1000)
setTimeout(arguments.callee, 50);
}
})(),
50
);
---


---
with({foo:0,bar:0}){
setTimeout(
function(){
window.status="foo:"+foo+", bar:"+bar;
if(foo++<1000 && bar-->-1000)
setTimeout(arguments.callee, 50);
},
50
);
}
---


HTH
Yep.
 
Reply With Quote
 
Antony Sequeira
Guest
Posts: n/a
 
      05-20-2004
Yann-Erwan Perio wrote:

> Antony Sequeira wrote:
>
>> While looking at some code I realized that the built in
>> setTimeout
>> function takes a string that is later
>> evaluated in the original caller's context.

>
>
> This original context is always the global object (window).

Ok. Thanks for correcting my mis-conception and for the example code.
-Antony

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
What can I use to write something like Console.WriteLine in a web servivce method Tony Johansson ASP .Net 2 01-09-2010 01:49 PM
Change setTimeout length. Or Pause or something. Knud Javascript 1 08-30-2004 02:57 AM
How to open a new console and write something out write printf() Peter Hansen C++ 1 08-24-2003 11:49 AM
How to open a new console and write something out write printf() Peter Hansen C Programming 1 08-24-2003 11:49 AM



Advertisments