Skip to main content

Javascript setTimeout() Function with Examples

setTimeout() Function

setTimeout() is a native JavaScript function. Which is allow us to call a specific JavaScript function or piece of code at specified interval or delay (on milliseconds). It might be useful when you want to display a message or a popup after user has browse the webpage for a certain amount of time.

Simple example for setTimeout

$('#btnOk').on('click', function(){
setTimeout(openPopup, 2000);

function openPopup()
alert('You press ok button');

in above example, it will display an alert message after 2 seconds once you press ok button.

Let’s have a look at the syntax for setTimeout function

var timerId = window.setTimeout(FunctionName, [timeInterval, parameter1, parameter2, ...]);
var timerId = window.setTimeout(jscode, [timeInterval]);

timerId is a numerical id return by setTimeout function, so later we can use it to call clearTimeout function to cancel the timer.

FunctionName – is the name of the JavaScript function to be executed after given interval.
jscode – in place of js function you can give direct the JS code to be executed
timeInterval – is the number of milliseconds by which the function should be delayed to call. If you are not passing the time interval then default it will be zero.

Difference between setTimeout vs window.setTimeout

If you see the above code carefully then you will have noticed that I have used window.setTimeout and title of this article is setTimeout, why is that?
let me give you the difference between both of them.

Well above both functions setTimeout and window.setTimeout are essentially same, There is a major difference like in window.setTimeout function we are referencing as a property of global window object. So sometimes if we have defined an another setTimout method in the same scope then it will be a issue. However what ever syntax you choose it’s up to the developer.

You can use setTimeout function in a following manner

using the function name

function LetsCall(){
alert('I am called.');
setTimeout(LetsCall, 2000);

using a variable which refers to the function

var LetsCall = function(){
alert('I am called.');
setTimeout(LetsCall, 2000);

using an anonymous function

alert('I am called.');
}, 2000);

using a direct JScode as a string

setTimeout('alert(\'I am called.\');', 2000);

Now lets move to the cancelling a Timer
The return value of setTimeout is a numerical id which can be used to cancel the timer using clearTimeout() function.

var timerId = setTimeout(jscode, 2000);


Creating and submitting a form using javascript

In this article I will show you how to create a form dynamically using JavaScript and submit it.

JavaScript provides a way to create a DOM element easily using createElement method.

see the below code snippet for creating a form element

var form = document.createElement('form');

using same method you can create other element as well see the below code which creates input element

var field1 = document.createElement('input');

After creating form element you can set the action, method, target attributes of the form element as below.

form.method = 'POST';
form.action = 'urlToPost'; = '_blank'

for filed element we need to set the name of the element and value of the element and need to add element to the form element as below = 'username';
field1.value = field1val;

after we add all the element to the form, we need to add form to the body of page and then submit the form as below


That’s it.

Now see the below complete function for Creating and submitting form using JavaScript.

function CreateandSubmitForm(urlToPost, field1val, field2val)
var form = document.createElement('form');
var field1 = document.createElement('input');
var field2 = document.createElement('input');

form.method = 'POST';
form.action = urlToPost; = 'username';
field1.value = field1val;
form.appendChild(field1); = 'password';
field2.value = field2val;



Introduction to Class in OOPS with

Understanding class

A class is like a blueprint. It allows us to create our own custom data types by grouping methods, events and variables of different types like int, string or any other custom types. Class defines the data and behavior of a type.

A class can be defined with a keyword “class” followed by the class name and body of the class with a pair of curly braces.
Let’s have a look at the general form of a class body.

below is the general form of a class body.

//Public is the access specifier while Calculator is the name of the class
public class Calculator
//variables declaration
private int var1;
private int var2;

//methods declaration
public decimal Add(decimal val1, decimal val2)
return val1+val2;

Classes supports inheritance while structs can’t support it.
Access specifiers plays an important role in the class definition, which specify the rules for accessing the member as well as the class itself. Default access specifier for the class is internal while for member it is private.

Data type specifies the type of variable, and return type specifies the data type of the data the method returns.

To access the class members we need to create an object of the class if class is not static, and using object name(dot)membername or member function name

See the below code for accessing Add method of Calculator class

Calculator calc = new Calculator();
decimal finalSum = calc.Add(10,20);