By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,652 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,652 IT Pros & Developers. It's quick & easy.

Get value returned by function called in setTimeout

P: 16
Hello Freinds,
I have a funtion to call from setTimeout function which will return some value..
I mean I need to get the value, the function called from the setTimeout function returns...
Please help
Oct 11 '07 #1
Share this Question
Share on Google+
7 Replies


P: 85
u can try something like this

Expand|Select|Wrap|Line Numbers
  1. function a()
  2. {
  3. var x="45";
  4. return x;
  5. }
  6. var y;
  7. setTimeout(y=a(),1000);
  8. alert(y);
  9.  
Oct 11 '07 #2

dmjpro
100+
P: 2,476
u can try something like this

Expand|Select|Wrap|Line Numbers
  1. function a()
  2. {
  3. var x="45";
  4. return x;
  5. }
  6. var y;
  7. setTimeout(y=a(),1000);
  8. alert(y);
  9.  

It will be ...

Expand|Select|Wrap|Line Numbers
  1. setTimeout("y=a()",1000);
  2.  
Debasis Jana
Oct 11 '07 #3

P: 16
u can try something like this

Expand|Select|Wrap|Line Numbers
  1. function a()
  2. {
  3. var x="45";
  4. return x;
  5. }
  6. var y;
  7. setTimeout(y=a(),1000);
  8. alert(y);
  9.  

I tried this out but the alert message is showing undefined for y value...
Oct 11 '07 #4

gits
Expert Mod 5K+
P: 5,390
I tried this out but the alert message is showing undefined for y value...
hi ...

of course i comment the code ... have a look at it:

Expand|Select|Wrap|Line Numbers
  1. // this function returns '45'
  2. function a() {
  3.     var x = "45";
  4.     return x;
  5. }
  6. // declare global var y
  7. var y;
  8.  
  9. // after 1 second y will be set to '45'
  10. setTimeout('y = a();', 1000);
  11.  
  12. // the alert is called instantly but y is not set yet!
  13. // so you get 'undefined'
  14. alert(y);
  15.  
to fix that you may use:

Expand|Select|Wrap|Line Numbers
  1. function a() {
  2.     var x = "45";
  3.     return x;
  4. }
  5.  
  6. var y;
  7.  
  8. function set_y () {
  9.     y = a();
  10.     alert(y);
  11. }
  12.  
  13. setTimeout(set_y, 1000);
  14.  
kind regards

ps: in case you use:

Expand|Select|Wrap|Line Numbers
  1. setTimeout(y = a(), 1000);
the timeout is useless ... y is assigned instantly the '45' from function a ... nothing more
Oct 11 '07 #5

P: 85
well yes but u can assign any function associated with it
it was just an example
and thanks gits for fixing my mistakes
i am also new to js
Oct 11 '07 #6

gits
Expert Mod 5K+
P: 5,390
well yes but u can assign any function associated with it
it was just an example
and thanks gits for fixing my mistakes
i am also new to js
heya rohitchawla,

no problem ... let me give a short additional explaination of the setTimeout method:

usage:

Expand|Select|Wrap|Line Numbers
  1. window.setTimeout(funcref, milliseconds);
funcref has to be a function-reference you may use the function-name here as in my example or the 'function-signature' enclosed in qoutes as in dmjpros example ... because it needs this reference since the code should be executed in the future ... and therefore has not to be called instantly ...

kind regards
Oct 11 '07 #7

acoder
Expert Mod 15k+
P: 16,027
Changed thread title to better describe the problem.
Oct 15 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.