Run a JavaScript function after a delay

/ #JavaScript


Sometimes you want to run a function a few seconds after the user has opened your website or similar. In this short guide, I'll show you how to do this.

An example

<script>
setTimeout(function() {
    alert('You waited 3 seconds!');
}, 3000);
</script>

So, when you open a website with this script and wait 3 seconds (3000 milliseconds), an alert will pop up on the screen.

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.