Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 25th, 2008, 08:42 AM
Authorized User
 
Join Date: Sep 2008
Location: Oslo, , Norway.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to peirix
Default getting a return value from setTimeout()

Is there a way of getting the return value from the function called within a setTimeout?

Something like this would have been great:
var timer = setTimeout("emailOutput = createEmail()", 50);


Reply With Quote
  #2 (permalink)  
Old November 25th, 2008, 09:24 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

No, the function hasn't run yet so the scenario doesn't make sense. Tthe return value is an id that can be used by cancelTimeout() to cancel the operation. To do what you want you could have emailOutput as a global variable that can be read after the delay has occurred.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #3 (permalink)  
Old November 25th, 2008, 11:13 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I could be wrong, Joe, but I think the answer you gave was really "yes."

Yes, the return value from createEmail( ) really is placed into that emailOutput variable.

But I suspect that the confusion peirix had/has is that he/she was expecting the value to be *immediately* available. That is:
Code:
    var timer = setTimeout("emailOutput = createEmail()", 50);
    if ( emailOutput == something ) ...
And THAT is the part that can't work, as Joe said.

The value of emailOutput won't be available until the timeout actually triggers, 50 milliseconds (a lifetime, in computer terms) from now.

So, peirix, if you need to *DO* something with that returned value, you might want to do:
Code:
    setTimeout("doSomethingWithValueReturnedFrom( assignToVariableOptional = createEmail() )", 50);
No real reason to assign the result of the call to createEmail() to a variable, but you could.
Reply With Quote
  #4 (permalink)  
Old November 26th, 2008, 03:55 AM
Authorized User
 
Join Date: Sep 2008
Location: Oslo, , Norway.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to peirix
Default

Thanks guys.

Then I guess I need to figure a way around assigning the value. The reason I need to have the timeout, is because I'm dynamically loading external scripts into a generated html-page. And the only way I could call functions inside them, is by using timeout (I'm guessing the reason is because they need time to be loaded into memory?).

But, yeah, Old, you were right. The reason I was confused, was of course the fact that I forgot how setTimeout worked, putting something on hold, and then jumping to the next line. For some reason, I was expecting it to linger at that line, until 50ms had passed.

Reply With Quote
  #5 (permalink)  
Old November 26th, 2008, 04:39 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

But why can't you just do as I suggested:

Suppose your existing code is like this:
Code:
step1( );
setTimeout( "emailTimeout=createEmail()", 50 );
step3( ...which uses that emailTimeout value ... );
Okay, so just change it to:
Code:
step1( );
setTimeout( "step2( )", 50 );
return;

// and then have your step2 look like this:
function step2()
{
    emailTimeout=createEmail();
    step3( ...which uses that emailTimeout value ... );
}
That is, delay processing the result of the call to createEmail() until the call is indeed complete.

No???
Reply With Quote
  #6 (permalink)  
Old November 26th, 2008, 05:47 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Or why can't you call the function after the document onload has fired? This should only occur after all scripts and images have been downloaded.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #7 (permalink)  
Old November 27th, 2008, 05:32 AM
Authorized User
 
Join Date: Sep 2008
Location: Oslo, , Norway.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to peirix
Default

@Joe: Well, because the Javascript I'm using to add external js-files is inside the body-tag, which I guess would be fired after the document onload in the first place, right?

@OldPedant: Yeah, that's what I ended up doing. Thanks. Works like a charm.

Reply With Quote
  #8 (permalink)  
Old November 28th, 2008, 12:44 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Quote:
quote:because the Javascript I'm using to add external js-files is inside the body-tag, which I guess would be fired after the document onload in the first place, right?
Well, yes and no.

The NON-TIMEOUT code will clearly be executed before the <BODY ONLOAD> fires, but any code deferred via setTimeout may or may not have yet executed. Can't really predict, and I don't think you should rely upon what you find to be true in any one browser, as another browser could act completely differently on this.

So I think Joe is wrong about calling from ONLOAD, but even if he is right for some browser, it's not a good idea to count on it. Opinion.
Reply With Quote
  #9 (permalink)  
Old November 28th, 2008, 03:58 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I'm often wrong but in this case I don't think so. What I meant was that any code run after the onload fires should, according to the standards and in both IE and Firefox, have access to all elements of the page including scripted functions.

--

Joe (Microsoft MVP - XML)
Reply With Quote
  #10 (permalink)  
Old November 28th, 2008, 06:22 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Oh, yeah. I read your answer differently than that.

Though, of course, you could use JS code to *add* elements to the page (either HTML or JS code), and that could occur at any time, including (for example) as the result of some user action long after the page has loaded.

Super silly example:
Code:
<html>
<head>
<script>
function showFoo( )
{
    var f = document.getElementById("Foo");
    if ( f == null ) alert("No such thing as Foo");
    else alert( f.innerHTML );
}
</script>
</head>
<body onLoad="showFoo( )">
<div id="Holder"></div>
<p>
Stuff here.
<p>
<script>
setTimeout( "document.getElementById('Holder').innerHTML = '<div id=Foo>This is Foo!</div>';", 5000 );
</script>
<a href="#" onClick="showFoo( ); return false;">showFoo</a>
</body>
</html>
But aside from stuff like that, yeah.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to set the maximum minutes in setTimeout() or kasipandian Javascript 2 February 18th, 2008 12:37 PM
setTimeOut with a zero parameter? calgarychinese ASP.NET 1.0 and 1.1 Basics 3 April 5th, 2007 08:44 PM
setTimeout second to Minute mateenmohd Javascript 6 November 28th, 2004 02:12 AM
Using setTimeout PortGuy Javascript 7 March 25th, 2004 02:06 PM



All times are GMT -4. The time now is 07:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.