0

How to make an object disappear after a certain amount of time using javascript

I am actually making a 3d snake game and I want the apple that I have made should disappear after 5 seconds. I know that I need to make a function that that includes the setTimeout(function() but I am not able to find how to implement this knowledge. Please help me out here !

25th Sep 2020, 11:04 AM
Harsh Shukla
Harsh Shukla - avatar
2 ответов
0
There will be needed a trigger / flag (information) that a apple had been created. In this time point starts the setTimeout for 5 seconds with function which will clear the game area. After that the game area will be new created of course without an apple.
25th Sep 2020, 11:32 AM
JaScript
JaScript - avatar
0
Harsh Shukla hello, Here you have exemple how to use setTimeout(). https://medium.com/@monica1109/scheduling-settimeout-and-setinterval-ca2ee50cd99f In your case you'll have to get the element in which is the apple. (Give an id="apple" to that element) Ex: let apple=document.querySelector("#apple"); Then in your setTimeout you can use the remove function on it. setTimeout(()=>{ apple.remove(); },5000)
25th Sep 2020, 11:35 AM
EmmanueLZ.
EmmanueLZ. - avatar