Search code examples
javascriptcsstiming

How can you execute a command for X seconds?


I've been trying to create an object which moves up and, after three seconds, comes back down, but I can only find code which tracks how long it takes until the command executes (ex. setInterval), rather than how long the code actually runs. Does anyone know how you'd bring the object back down after a specified time interval?

addEventListener("keyup", () => {
            char.style.transition = "0.3s ease";
            char.style.transform = "translateY(50%)";
        });

Solution

  • setTimeout(function(){ 
        alert("bring down object"); 
        //function that brings out object goes here
    }, 3000);
    

    3000 is the amount of time to wait before running the function its default is in milliseconds so here it waits for 3 seconds before running the function manipulate that number to fit your required wait time before running the function that brings back the object.