Executing JavaScript After X Seconds

Executing JavaScript after X seconds

I believe you are looking for the setTimeout function.

To make your code a little neater, define a separate function for onclick in a <script> block:

function myClick() {
setTimeout(
function() {
document.getElementById('div1').style.display='none';
document.getElementById('div2').style.display='none';
}, 5000);
}

then call your function from onclick

onclick="myClick();"

How to run a JavaScript function after x seconds

<script>
window.onload = function(){
//time is set in milliseconds
setTimeout(div_show, 10000)
};
</script>

How can you execute a command for X seconds?

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.

If after x seconds call function

You setInterval function.

setInterval(function_name, time_in_milli_sec);

Call JavaScript function after 1 second One Time

If it needs to run just once you can use setTimeout

setTimeout(function () {
console.log('Hello world')
}, 1000)

setTimeout(() => {
console.log('Foo bar')
}, 1000)

Javascript: Call a function after specific time period

You can use JavaScript Timing Events to call function after certain interval of time:

This shows the alert box every 3 seconds:

setInterval(function(){alert("Hello")},3000);

You can use two method of time event in javascript.i.e.

  1. setInterval(): executes a function, over and over again, at
    specified time intervals
  2. setTimeout() : executes a function, once, after waiting a
    specified number of milliseconds

Javascript + Run a function for X seconds

I see these easiest approaches to this:

  1. If Function A is asynchronous (e.g. sending an AJAX request and waiting for reply), simply use setTimeout, as the request will be done in the background and not freeze the main process. So funcA() will be immediately done running on the UI thread, then the next expression will immediately take place, (which waits for 5 seconds), then the callback from the AJAX response will be called whenever it's done fetching the response.

    funcA();
    setTimeout(funcB, 5000);
  2. If Function A takes up to 5 seconds for other reasons, and you need the wait to be more dynamic, you could probably time it and subtract:

    var startTime = new Date().getTime(), endTime;
    funcA();
    endTime = new Date().getTime();
    setTimeout(funcB, 5000 - (endTime - startTime));
  3. If Function A might take longer than 5 seconds, and Function B needs to wait for it, I suggest something similar to this:

    function funcA() {
    var startTime = new Date().getTime(), endTime;
    // ... code ...
    $.post(..., {
    ...,
    // when done/resolved the AJAX request ->
    success: function(response) {
    endTime = new Date().getTime();
    setTimeout(funcB, 5000 - (startTime - endTime));
    }
    });
    }


Related Topics



Leave a reply



Submit