471,573 Members | 1,279 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

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 1552
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
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.