setTimeout and setInterval in java script

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);
© 2016 - 2022, All Rights are Reserved.