Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (6.1k points)
I am new to Javascript. Please tell me about setTimeout and setInterval methods.

1 Answer

0 votes
by (11.7k points)
edited by

 Settimeout Method: In this method, a function is executed exactly when the specified waiting time is over. This is a method of HTML DOM window object. 

Syntax: window.setTimeout(function, milliseconds);

The first parameter is an executable function.

The second parameter is the time in millisecond which indicates that after a certain period of time the request will be processed. 

Example:

<button onclick="setTimeout(myFunction, 4000)">Click me</button>

<script>

function myFunction() {

  alert('This is Intellipaat');

}

</script>

SetInterval Method: This method repeats a specific function after every given time interval. 

Syntax: window.setInterval(function, milliseconds);

 First parameter is a function which needs to be executed.

Second parameter is a time interval in milliseconds where the specific function will be executed after a given time. 

Code:

var myVar = setInterval(myTimer, 2000);

function myTimer() {

  var a = new Date();

  document.getElementById("demo").innerHTML = a.toLocaleTimeString();

}

Want to become an expert in web technology, check out the Web Developer Certification courses offered by Intellipaat. 

Related questions

0 votes
1 answer
asked Feb 26, 2021 in Web Technology by Jake (7k points)
0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked May 4, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
asked Apr 15, 2021 in Java by sheela_singh (9.5k points)

Browse Categories

...