Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Basic JavaScript

JS Tutorial JS Introduction JS Where To JS Output

JS Syntax

JS Syntax JS Statements JS Comments JS Variables JS Let JS Const JS Types

JS Operators

JS Operators

JS If Else

JS If Conditions

JS Loops

JS Loops

JS Strings

JS Strings

JS Numbers

JS Numbers

JS Functions

JS Functions

JS Objects

JS Objects

JS Scope

JS Scope

JS Dates

JS Dates

JS Temporal

JS Temporal  New

JS Arrays

JS Arrays

JS Sets

JS Sets

JS Maps

JS Maps

JS Iterations

JS Loops

JS Math

JS Math

JS RegExp

JS RegExp

JS DataTypes

JS Data Types

JS Errors

JS Errors

JS Debugging

JS Debugging

JS Conventions

JS Style Guide

JS Reference

JS Statements

JS Projects

JS Projects New

JS Versions

JS 2026

JS HTML

JS HTML DOM JS Events

JS Advanced

JS Functions JS Objects JS Classes JS Asynchronous JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JavaScript Timing Events

Timing Events let you run code:

  • After a Delay
  • Or Repeatedly

Timing is driven by Timing Events generated by the system clock.

Timer Fuctions

FunctionDescription
setTimeout()Sets a clock timeout (runs once)
setInterval()Sets a clock interval (runs repeatedly)
clearTimeout()Clears a timeout
clearInterval()Clears an inteval

The timer functions belong to the window object.

setTimeout() is the same as window.setTimeout().


A Clock

Example

<p id="clock"></p>

<script>
// Call showTime every 1000 millisec
setInterval(showTime, 1000);

// Fuction to display the time
function showTime() {
  const d = new Date();
  document.getElementById("clock").innerHTML =
  d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
}
</script>
Try it Yourself »

The setTimeout() Method

The setTimeout() method executes a function after a delay in milliseconds.

setTimeout(function, delay, p1,...,pN)

Parameters

Parameter Description
function Required.
The function to be called when the timer expires.
delay Optional.
The milliseconds the timer should wait.
Defalt 0 (immediately).
p1,...,pN Optional.
Arguments passed to the function.

Return Value

Type Description
IntegerAn integer that uniquely identifies the timer.
This "timeout ID" can later be passed to clearTimeout() to cancel the timer.

Example

<button id="btn">Start</button>

<p id="demo"></p>

<script>
// let btn listen for a click
document.getElementById("btn").addEventListener("click", function () {
// Then call showMsg after 2 seconds
  setTimeout(showMsg, 2000);
});

// Function to display message
function showMsg() {
  document.getElementById("demo").innerHTML = "Hello after 2 seconds!";
}
</script>
Try it Yourself »

The setTimeout() method is a core part of asynchronous JavaScript, allowing code execution to be scheduled without blocking the main execution thread.



The SetInterval()Method

The setInterval() method calls a function repeatedly.

setInterval(function, delay, p1,...,pN)

Parameters

Parameter Description
function Required.
The function to be called for every interval.
delay Optional.
The milliseconds between each function call.
Defalt 0.
p1,...,pN Optional.
Arguments passed to the function.

Return Value

Type Description
IntegerAn integer that uniquely identifies the timer.
This "timeout ID" can later be passed to clearInterval() to cancel the timer.

Example

setInterval() used with clearInterval():

<button id="start">Start Counter</button>
<button id="stop">Stop</button>

<p id="counter">0</p>

<script>
let myInterval;
let count = 0;

const btnStart = document.getElementById("start");
const btnStop = document.getElementById("stop");

// let btnStart listen for a click
document.getElementById("start").addEventListener("click", function () {
// Then start counter
  myInterval = setInterval(counter, 1000);
});

// let btnStop listen for a click
document.getElementById("stop").addEventListener("click", function () {
// Then stop counter
  clearInterval(myInterval);
});

function counter() {
  count++;
  document.getElementById("counter").innerHTML = count;
}
</script>
Try it Yourself »

The setTimeout() method executes a function only once after a delay.

The setInterval() method executes a function repeatedly at every interval.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->