Back to Top

The Difference Between SetInterval and SetTimeout in JavaScript

SetInterval and SetTimeout

In this article, I will introduce setInterval () and setTimeout () functions of JavaScript and are call for execution when the usage.setTimeout () and setInterval () is generally used to deal with the delay and timing tasks.

The JavaScript library functions setTimeout and setInterval are which delayed seemingly parallel execution rather than the actual spawning of processes.You might like other article using setInterval here jQuery Shake Effect Explained

Whether you use setTimeout or setInterval, but when use the function name as a call handler can not take parameters.In many cases have to take parameters to set timeout or interval.

SetInterval

SetInterval very handy to use because it set the delay function in JavaScrpt.

Explanation:

The timing of implementation of the function call, in accordance with a specified period (in milliseconds), to call a function or evaluate an expression.

SetInterval is loaded from a specific interval of time to perform an expression changes automatically display using setInterval achieve a random number.

Also Read: Convert date from one format to another format using javascript

SetInterval function to be executed at time intervals (without “()”) and allows to define the function parameters called Interval. SetInterval has two ways to call.

  1. Direct call

SYNTAX:

setInterval (function, interval); 
  1. Indirect Reference

SYNTAX

setInterval (Object, "methodName", interval)

To call a function or code strings to be executed.interval must. Periodic execution time or interval between calls code in milliseconds.

Code Explanation:


As above code, the function setInterval will be called at the specified intervals, in this case, it will be called at every 1000 milliseconds. The second argument is the Interval to the passed to the function. To see the effect that after the function is called, the output window will see Interval called.

NOTE: setInterval () method will continue to call the function until the clearInterval () is called, or the window is closed.

SetTimeOut

SetTimeout very similar. setTimeout used to delay then perform an operation.

The settimeout () method is used to call the specified number of milliseconds after the function or evaluate an expression.

SYNTAX

setTimeout (function, milliseconds) 

The function is required and it will be used after the call to execute JavaScript code. The second parameter is also required and value will be the number of milliseconds to wait before executing the code.

NOTE: setTimeout () code executed only once. If you want continuous call, use the setInterval ().

Code Explanation:

var time = 1000;//Interval (in milliseconds)		
var id = setTimeout(function() {
		//Here is the function or expression will be call	
	}, time);

ClearInterval

ClearInterval is mainly for clearing the interval set by setInterval.

SYNTAX:

clearInterval (id_of_setinterval)

Parameter passed in clearInterval() function must be ID value returned by the setInterval() function.This value identifies cancel delay implementation of the code block.

To cancel setInterval (), function set the timing of the implementation of the operation.

Code Explanation:

Hope this post clear your query or clear the difference between SetTimeout and SetInterval.

Comments (2)

  1. Thanks for the article – very usefull info that helped me. Regards

  2. jishnu chandran says:

    Good one, Setinterval() is very helpfull for me by solving JQwidget GRID focusing problem 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Newly Approved Joomla Module

Posted on 5 years ago

Bhumi

Hello world

Updated 1 week ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi