0

Why won't the timeout work?

I was making a timeout using JS in my code but the timeout won't call the function when the time is up. Please explain how to fix it https://sololearn.com/compiler-playground/WBb8gMkuf74p/?ref=app

1st Dec 2024, 8:31 PM
☕︎︎ AstroBirb ✦
☕︎︎ AstroBirb ✦ - avatar
8 Respuestas
+ 6
This code is where the issue is: <script> setTimeout(trigger(), 20000) var events = ["money", "bankruptcy", "orange", "poison", "nothing"]; setTimeout(function trigger(), 20000); When you call setTimeout - you should pass the name trigger, but not (). You should also remove the word "function". So those two lines have to be updated. I'm not sure if you intend to set the trigger twice. Probably another mistake. When you pass a function as an argument, don't pass the () with it. When you use () the function executes and it's return value is substituted as a parameter. In this case, you actually want to pass the function's address so you don't use the (). And you probably don't need the setTimeout in there twice either. So do this instead: <script> setTimeout(trigger, 20000) var events = ["money", "bankruptcy", "orange", "poison", "nothing"];
1st Dec 2024, 11:06 PM
Jerry Hobby
Jerry Hobby - avatar
+ 5
☕︎︎ AstroBirb ✦ It's still not right. your trigger function is placed inside a setTimeout. But your button is calling trigger directly. This will bypass the setTimeout. you should put the setTimeout inside another function and call that function instead. Then the setTimeout will be called and that will call trigger after 20 seconds. <button id="biggraybutton" onclick="activate()"></button> <script> function activate(){ setTimeout(trigger, 20000); } /* function trigger(){...} and other codes */ </script> I played some more with your code. https://sololearn.com/compiler-playground/WLhDBDNt1ID7/?ref=app
2nd Dec 2024, 1:13 AM
Bob_Li
Bob_Li - avatar
+ 4
Where's the time out?
1st Dec 2024, 9:44 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
Oh I'm so sorry that's the wrong code lol https://sololearn.com/compiler-playground/W1f3nOoz44xe/?ref=app
1st Dec 2024, 10:46 PM
☕︎︎ AstroBirb ✦
☕︎︎ AstroBirb ✦ - avatar
+ 2
Thank you it worked 😁
1st Dec 2024, 11:07 PM
☕︎︎ AstroBirb ✦
☕︎︎ AstroBirb ✦ - avatar
+ 2
Thank you Bob_Li for fixing my code and improving it, I will give credit 😁
2nd Dec 2024, 3:51 AM
☕︎︎ AstroBirb ✦
☕︎︎ AstroBirb ✦ - avatar
+ 1
In the <script> tags
1st Dec 2024, 10:45 PM
☕︎︎ AstroBirb ✦
☕︎︎ AstroBirb ✦ - avatar
0
How can I build a database for my website
2nd Dec 2024, 4:39 AM
bwambale chricent
bwambale chricent - avatar