469,306 Members | 2,121 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

how to write something like setTimeout

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

Jul 23 '05 #1
7 1490
Lee
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?

Jul 23 '05 #2
>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.


Google is your friend.

http://groups.google.com/groups?q=jc...ript&start=10&
hl=en&lr=&ie=UTF-8&group=comp.lang.javascript&selm=347489c2.3549165 8%40new
s.teaser.fr&rnum=15

Peace, Vm
Yaz

Providing complicated solutions to simple problems since 1997.
Jul 23 '05 #3
>>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.


Google is your friend.


But the AOL newsgroup reader isn't. :)

groups.google.com/groups?q=jcs+group:comp.lang.javascript&start=10&h l=en&l
r=&ie=UTF-8&group=comp.lang.javascript&selm=347489c2.3549165 8%40news.tease
r.fr&rnum=15

You'll have to join each of those lines together into the Address Box in your
browser.

Peace, Vm
Yaz

Providing complicated solutions to simple problems since 1997.
Jul 23 '05 #4
I'll avoid posting longs URLs in the future. Just do a search in Google Groups
using this.

fooGlobal group:comp.lang.javascript

Peace, Vm
Yaz

Providing complicated solutions to simple problems since 1997.
Jul 23 '05 #5
On 20 May 2004 05:01:37 GMT, ju*****@aol.compels.me (to heave chunks)
wrote:
I'll avoid posting longs URLs in the future. Just do a search in Google Groups
using this.

fooGlobal group:comp.lang.javascript


Another solution is to use something like tinyurl.com, which turns
your monsterous URL into http://tinyurl.com/24oar

Much easier to deal with.
Jul 23 '05 #6
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.
Jul 23 '05 #7
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

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Charles Crume | last post: by
2 posts views Thread by Athanasius | last post: by
6 posts views Thread by Brent | last post: by
12 posts views Thread by Andrew Poulos | last post: by
4 posts views Thread by E | last post: by
1 post views Thread by mike | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.