Using the JavaScript function setTimeout ()

Time is an important factor in any application, especially when programming web resources. The browser language provides the developer with two options: JavaScript setTimeout - a one-time launch of a function after a certain period of time, and setInterval - a multiple launch of a function after equal periods of time.

javascript settimeout

The setTimeout function allows you to organize temporary processes dynamically by adjusting the time intervals and executable code.

Syntax and Function Usage

JavaScript setTimeout contains two required parameters: the function to be executed and the time delay before it is executed. Instead of a function, you can specify a line of code, but this is not practiced. The desired algorithm is preferably performed in the function format.

The result of calling window.setTimeout JavaScript defines a numerical value by which you can reset the set delay time and not execute the code written in the function.

window settimeout javascript

In this example construction:

  • dLine.innerHTML = '(x) <br/>';

Recorded specifically to show that the execution of a sequence of commands is not interrupted by calls:

  • setTimeout ();

JavaScript language only launches wait timeouts, after which it then performs the corresponding functions. The result of the above code is shown on the right below.

javascript settimeout in loop

All functions worked after the set time: 1, 2 and 7 seconds. The function, which was supposed to work after 4 seconds, was reset and was not executed.

Using setTimeout in a loop

JavaScript is a fast and efficient language. Loop operators do not represent any delay in the vast majority of cases. It is important to note that the algorithm in which JavaScript calls to setTimeout will not always be executed as you would expect without experience with this function.

You should not give importance to the pause settings in addition to their direct purpose: a pause is the time before the function starts. What this function will do is indirectly related to the main algorithm.

Especially important: the use of JavaScript setTimer in a loop, in any other block of statements or a sequence of commands has nothing to do with the pause being formed.

window settimeout javascript

A pause is assigned for the duration of the function that is recorded in it. This is essential. Using a JavaScript call to setTimeout in a loop of ten iterations, the developer pauses ten times before ten calls to the same function.

Since the loop is very fast, these ten calls are actually ten times the call of the same code at the same time.

Scopes of JavaScript setTimeout

The web resource must monitor the behavior of visitors in order to properly fulfill its mission, ensure security, send messages and for other reasons.

For example, a site, which is an administrative resource for managing employees, should provide information to the management about the current state of business activity of the company, which of the employees went to work, who does what, etc.

If the work of employees is controlled through a website on a global network, then certain pages of the office site will be loaded into the browsers of their computers. Each such page can send data to the server about which employee has connected and what it does.

javascript settimeout in loop

In modern business, many companies allow employees to use their own devices (mobile phones, smartphones, tablets). Company management may have access to an employee who is on the road, on a business trip, on vacation.

The realization of such an opportunity can only be achieved through periodic exchange of information between local devices and the server, and it is not at all necessary that the exchange should be organized at regular intervals. The protocol for providing comprehensive communication depends on the characteristics of the business and business activity of the company.


All Articles