473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Get value returned by function called in setTimeout

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
7 20216
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
2,476 2GB
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
16,027 Expert Mod 8TB
Changed thread title to better describe the problem.
Oct 15 '07 #8

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

Similar topics

20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
2
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another...
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
2
by: Noggon | last post by:
My function, called first() keeps coming back to me in the javascript console as being undefined. Yet I can't think why it isn't loaded. Maybe I'm brain dead, so could use some help! :-) What...
3
by: Divick | last post by:
I was reading this section in Bruce Eckel's book which talks about passing and returning large objects ( Chapter 11: References & the Copy-Constructor ), where he explains that how to return big...
0
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I want to display a progress bar on my web page. I have this code for progress bar in javascript in my web page. I have a button on the web page, if someone clicks that button...
12
by: ed | last post by:
Hi there, I just remembered this from a few days ago, and was looking for a reason for it. I was trying to call a function as so: addEvent( whatever, whatever, functionName() ); and as I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.