459,652 Members | 1,416 Online
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
7 Replies

 P: 85 u can try something like this Expand|Select|Wrap|Line Numbers function a() { var x="45"; return x; } var y; setTimeout(y=a(),1000); alert(y);   Oct 11 '07 #2

 100+ P: 2,476 u can try something like this Expand|Select|Wrap|Line Numbers function a() { var x="45"; return x; } var y; setTimeout(y=a(),1000); alert(y);   It will be ... Expand|Select|Wrap|Line Numbers setTimeout("y=a()",1000);   Debasis Jana Oct 11 '07 #3

 P: 16 u can try something like this Expand|Select|Wrap|Line Numbers function a() { var x="45"; return x; } var y; setTimeout(y=a(),1000); alert(y);   I tried this out but the alert message is showing undefined for y value... Oct 11 '07 #4

 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 // this function returns '45' function a() {     var x = "45";     return x; } // declare global var y var y;   // after 1 second y will be set to '45' setTimeout('y = a();', 1000);   // the alert is called instantly but y is not set yet! // so you get 'undefined' alert(y);   to fix that you may use: Expand|Select|Wrap|Line Numbers function a() {     var x = "45";     return x; }   var y;   function set_y () {     y = a();     alert(y); }   setTimeout(set_y, 1000);   kind regards ps: in case you use: Expand|Select|Wrap|Line Numbers 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

 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 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

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