[js] setTimeout and return | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

[js] setTimeout and return

how can I do function a(){ return 'abc'; } return setTimeout(a(),1000); ? it occurs me an error...

13th Apr 2018, 10:19 AM
NoxFly
NoxFly - avatar
27 Answers
+ 2
yes
13th Apr 2018, 10:44 AM
NoxFly
NoxFly - avatar
+ 2
not using jquery
13th Apr 2018, 10:44 AM
NoxFly
NoxFly - avatar
+ 2
it occurs me an error
13th Apr 2018, 10:46 AM
NoxFly
NoxFly - avatar
+ 2
setimeout(a,1000); ?
13th Apr 2018, 10:50 AM
NoxFly
NoxFly - avatar
+ 2
it works for my code when in setTimeout I write msg.channel.send() but when it's return, there is not an error but nothing appears
13th Apr 2018, 10:59 AM
NoxFly
NoxFly - avatar
+ 2
let n = msg.content.split('dice ')[1]; if(n<1 || n>6 || n != Math.round(n)) { return 'The number need to be an integer between 1 and 6'; } if(!(/\d+/.test(n))) { return 'Need to be a number !'; } let r = Math.round(Math.random()*5+1); let res = (r==n)?'You win':'You lose'; function a() { return'Your number: '+n+'\nDice result : '+r+'\n'+res; } return setTimeout(a,1000); }
13th Apr 2018, 11:01 AM
NoxFly
NoxFly - avatar
+ 2
if you replace the return on the setTimeout by msg.channel.send() its good
13th Apr 2018, 11:02 AM
NoxFly
NoxFly - avatar
+ 2
but I want return
13th Apr 2018, 11:02 AM
NoxFly
NoxFly - avatar
+ 2
I will try
13th Apr 2018, 11:06 AM
NoxFly
NoxFly - avatar
+ 2
function a() { return'Your number: '+n+'\nDice result : '+r+'\n'+res; }setTimeout(a,5000); return a(); it return immediatly
13th Apr 2018, 11:18 AM
NoxFly
NoxFly - avatar
+ 2
nope......... 😭😭😭 sorry I think I will forgot this idea
13th Apr 2018, 11:27 AM
NoxFly
NoxFly - avatar
+ 2
oh it work but... it print 2 time, one immediatly and a second time 5 sec after
13th Apr 2018, 11:28 AM
NoxFly
NoxFly - avatar
+ 2
so ... window.onload...
13th Apr 2018, 11:31 AM
NoxFly
NoxFly - avatar
+ 1
why are you using setTimeout in return?
13th Apr 2018, 10:36 AM
Akib
Akib - avatar
+ 1
I'm programming discord bot in js. so when I write return a; my bot write a. when I write msg.channel.send(b); return a; my bot write b a so I want a cooldown btw b and a
13th Apr 2018, 10:39 AM
NoxFly
NoxFly - avatar
+ 1
You want to delay the return?
13th Apr 2018, 10:43 AM
Akib
Akib - avatar
+ 1
then use it below the function. Then call it.
13th Apr 2018, 10:45 AM
Akib
Akib - avatar
+ 1
remove a() from the timeout just put a
13th Apr 2018, 10:50 AM
Akib
Akib - avatar
+ 1
yap
13th Apr 2018, 10:51 AM
Akib
Akib - avatar
13th Apr 2018, 10:56 AM
Akib
Akib - avatar