Why this function with setTimeout is returning the output as 'undefined'? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why this function with setTimeout is returning the output as 'undefined'?

function wait(value, timespan) { setTimeout(function() { return value; }, timespan); } var n = wait(5, 1000); //undefined

15th Mar 2019, 10:26 PM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
9 Answers
+ 3
Luis Febro 🇧🇷 I think I figured it out. Maybe because at the time of function call the value is not yet set. Check out my code: function wait(value, timespan) { setTimeout(wait,timespan,value); return value; } var n = wait(5, 1000); function output() { alert(n); } https://code.sololearn.com/WcWpgB5Efw0w/?ref=app
16th Mar 2019, 12:18 AM
Pete Wright
Pete Wright - avatar
+ 6
Luis Febro 🇧🇷 The code is working fine for me so it's probably the device you are viewing it on that doesn't have support it. To be honest though your solution is much better anyway especially for browser compatibility so I'd recommend using that instead.
17th Mar 2019, 9:03 PM
LynTon
LynTon - avatar
+ 5
Because the code won't stop for the setTimeout function to run your n variable may not have a value if you try to use it immediately after is declared. e.g. var n = wait(5, 1000); // Takes 1s to define console.log(n); // Runs 100ms after declaration
16th Mar 2019, 12:27 AM
LynTon
LynTon - avatar
+ 5
(Sorry, took me awhile to sort an hitch in this code) In the second section of your code the "setTimout" in the function isn't actually doing anything and the value is still immediately returned. The first thing that comes to mind would be to use the ES7 await/async syntax and the ES6 Promise syntax so the code would be paused while the wait function runs: https://code.sololearn.com/WM6L2TxLmdV6/?ref=app
16th Mar 2019, 3:33 AM
LynTon
LynTon - avatar
+ 4
Pete Wright I created this code with setInterval and SetTimeout timer functions. There is a little glitch in the code where you find 'not working' but if you stop before the countdown is up, no bug is found. Moreover, this unworking chunk of code aims to delete all elements but for now it only deletes the first one. But it supposed to happen after 30 seconds though. Check it out: https://code.sololearn.com/WxqKxYlKbqkM/?ref=app
17th Mar 2019, 6:57 PM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 3
LynTon Yeah. You may be right. But I have the latest version of this app. But to be honest, my mobile is 2 years old really. It is getting old dawn right. Heh Yeah, Es7 is far much uncomplicated and got a cleaner syntax but backward compability is still an issue in the WWW. Nevertheless, I have been learning Babeljs which compiles the most recently added features - including Es7, ESNext ones - into Es5, the most compatible version of JS which is suitable along with both for old and modern browsers. It is really an amazing tool.
18th Mar 2019, 12:34 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 2
LynTon it seems that Es7 and latest JS's syntaxes are not working in SoloLearn playground. Thus your code is not working. Pops up a bug when pushing the button. Maybe a Babeljs-like solution would suit well for this platform to enable these new syntaxes. But I do not know how workable it should be to implement it properly.
17th Mar 2019, 7:04 PM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 1
Thanks for clarification of yours. Now I got it.
16th Mar 2019, 1:35 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 1
LynTon Now I am confused. When I set the timespan in my code to let's say 5000, it still alerts the value immediately without waiting. Is there any way in JS to delay a variable assignment?
16th Mar 2019, 1:55 AM
Pete Wright
Pete Wright - avatar