top

Search

JavaScript Tutorial

There are sometimes that we need to execute JavaScript code in time-intervals. These are called Timing Events.There are two timing events method in Javascript –setTimeout allows running a function once after an interval of time.setInterval allows running a function regularly with the interval between the runs.These methods are actually not a part of JavaScript language but supported by all browsers and NodeJS.setTimeout method in JavascriptThe syntax is as below –setTimeout(func, delay[, arg1, arg2..])Func- h3Anonymous callback Function to be executed.Delay- h3The delay before run, in milliseconds (1000 ms = 1 second).Arg1, arg2…- h3Optional Arguments for the function (not supported in IE9-)Let jump into examples.The below code will run the function sayJS() after 10 seconds. It will show an alert pop-up after 10 seconds.function sayJS() {  alert('Hello JS'); } setTimeout(sayJS, 10000);We can also use it with the optional arguments.function sayJS(lang, role) {  alert(lang + ' ' + role); } setTimeout(sayJS, 10000, "JavaScript", "Developer");setInterval method in JavascriptThe syntax is as belowsetInterval(func, delay[, arg1, arg2..])All arguments have the same meaning as setTimeout. But setInterval runs regularly after the given interval of time.To stop further calls, we should call clearInterval(timerId). Let’s look at the complete example.// repeat with the interval of 2 seconds let timerId = setInterval(() => alert('Hello JS'), 2000); // after 10 seconds stop setTimeout(() => { clearInterval(timerId); alert('stoped...'); }, 10000);Here we are using the arrow function to call the callback function inside only. Also, notice we have to use the setTimeout for clearInterval or else the browser won’t know when to run clearInterval.
logo

JavaScript Tutorial

JavaScript Timing Events

There are sometimes that we need to execute JavaScript code in time-intervals. These are called Timing Events.
There are two timing events method in Javascript

  • setTimeout allows running a function once after an interval of time.
  • setInterval allows running a function regularly with the interval between the runs.

These methods are actually not a part of JavaScript language but supported by all browsers and NodeJS.

setTimeout method in Javascript

The syntax is as below –

setTimeout(func, delay[, arg1, arg2..])

Func- h3

Anonymous callback Function to be executed.

Delay- h3

The delay before run, in milliseconds (1000 ms = 1 second).

Arg1, arg2…- h3

Optional Arguments for the function (not supported in IE9-)

Let jump into examples.
The below code will run the function sayJS() after 10 seconds. It will show an alert pop-up after 10 seconds.

function sayJS() {
 alert('Hello JS');
}
setTimeout(sayJS, 10000);

We can also use it with the optional arguments.

function sayJS(lang, role) {
 alert(lang + ' ' + role);
}
setTimeout(sayJS, 10000, "JavaScript", "Developer");

setInterval method in Javascript

The syntax is as below

setInterval(func, delay[, arg1, arg2..])

All arguments have the same meaning as setTimeout. But setInterval runs regularly after the given interval of time.

To stop further calls, we should call clearInterval(timerId). Let’s look at the complete example.

// repeat with the interval of 2 seconds
let timerId = setInterval(() => alert('Hello JS'), 2000);
// after 10 seconds stop
setTimeout(() => { clearInterval(timerId); alert('stoped...'); }, 10000);

Here we are using the arrow function to call the callback function inside only. Also, notice we have to use the setTimeout for clearInterval or else the browser won’t know when to run clearInterval.

Leave a Reply

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

Comments

Aditya

The tutorial provides the more information JavaScript this provides the end to end information about JS and also they provides the more detailed concepts of loops, functions, objects, arrays this will use us to buld the effective web site. Thanks for the team good job.

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More