ES6
June 3, 2026 1 min read

setTimeout and setInterval in java script

HS
HEMANT SINGH
Technical Writer & Educator

setTimeout()

Syntax

setTimeout(function_name, delay_timein_milliseconds, arg1, arg2);

Example

function msg(){
console.log("welcome to capscom technology")
}
setTimeout(msg, 2000);

setTimeout with argument

function msg(text){
console.log(text)
}
setTimeout(msg, 2000, "welcome to capscom technology");

cancel setTimeout

function msg(text){
console.log(text)
}
let x= setTimeout(msg, 2000, "welcome to capscom technology");
clearTimeout(x);

setInterval()

Syntax

setInterval(function_name, time_interval_in_milliseconds, arg1, arg2);

Example

function msg(){
console.log("welcome to capscom technology")
}
setInterval(msg, 2000);

setInterval with argument

function msg(text){
console.log(text)
}
setInterval(msg, 2000, "welcome to capscom technology");

cancel setInterval

function msg(text){
console.log(text)
}
let x= setInterval(msg, 2000, "welcome to capscom technology");
clearInterval(x);