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
8 Answers
+ 4
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"];
+ 3
Where's the time out?
+ 3
☕︎︎ 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
+ 2
Oh I'm so sorry that's the wrong code lol
https://sololearn.com/compiler-playground/W1f3nOoz44xe/?ref=app
+ 1
In the <script> tags
+ 1
Thank you it worked 😁
+ 1
Thank you Bob_Li for fixing my code and improving it, I will give credit 😁
0
How can I build a database for my website