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.
setInterval()
: executes a function, over and over again, at
specified time intervalssetTimeout()
: 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:
If
Function A
is asynchronous (e.g. sending an AJAX request and waiting for reply), simply usesetTimeout
, as the request will be done in the background and not freeze the main process. SofuncA()
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);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));If
Function A
might take longer than 5 seconds, andFunction 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
Fixed/Absolute Positioning Neglected in iOS When Focusing on Input
Countdown Timer 'Delays' When Tab Is Inactive
Detect When a Position: Fixed; Element Crosses Over Another Element
How to Add Alpha Filter to Any HTML Element and Keep the Other Filters in Ie
Angular 2 - How to Dynamically Change an Entire CSS Stylesheet Based on Url Queryparams
How to Redefine CSS Classes with JavaScript
How to Get the Number of Children Div in CSS or SASS
Javascript: Get Typed CSS Values Not Computed Values
Change Width Proportions of Two Blocks with a Slider
Position Fixed - Horizontal Scroll
How to Keep Input Placeholder Visible When User Is Typing
Using Document Object in Nodejs
Jquery Draggable Event Changing the CSS of Child Element
Load Mobile CSS If User Is on Android
Vue Transition Not Triggering on Button Click
Switch Window Between Normal and Full-Screen Mode
How to Increase Width of Morris.Js Bar or Add Space Between Bars