JavaScript time management setInterval ()

The development of Internet technologies has led to the fact that the page is fully loaded only when the visitor arrives or the client logs into the web resource. Further work is done in an AJAX or similar environment.

javascript setInterval

All browsers allow the onload event, which occurs after the page is loaded into the browser and all DOM objects take their places in it. Appointing a handler for this event is the right decision, and starting a web resource timer in it is the natural beginning of work with a visitor or client.

Syntax and meaning of setInterval () function

The setInterval () JavaScript function has only two parameters: the code and the time after which this code needs to be re-executed. The result of the function is a unique number, referring to which you can stop the execution of the function using clearInterval ().

Until the command is given to stop, and the page from which the JavaScript setInterval () was launched is not closed, the code will be executed at regular intervals.

The time interval is set by a numerical value in milliseconds from the calculation:

  • 1 second is 1000 milliseconds.

Usually the first parameter indicates not a line of code, but an unnamed function. Such a rule is generally accepted, although the developer may do so as is convenient for the optimal solution of the problem of temporary control of processes.

A practical example of using a function

The site is under construction. Scope - the creation of documents in MS Word format, designed according to the standards of theses and term papers, dissertations or electronic document management requirements.

javascript setinterval stop

The site is focused on reading and converting an undetermined number of files created by unprofessional users, and therefore containing undefined options for incorrect formatting in advance. Recording recognition time is of particular importance, not counting security issues and monitoring user behavior.

creeping line in javascript setinterval text box

Here in the tag (1): creeping line in the text box. JavaScript setInterval () continuously, with a frequency of 1 second, forms the current time. The tags (2) and (3) reflect the code of the visitor and the session of the logged in (registered) client and his session.

On the right, the state of the main variables is dynamically reflected for the purpose of page debugging.

The JavaScript function setInterval () is launched in the GoPage () function, which is triggered by the onload event, which occurs after the entire page loads.

This site is single-page, all of its elements are formed in time depending on the behavior of the visitor (client) using AJAX technology.

Implementation of the JavaScript function setInterval () is performed by obtaining the current time, formatting it, and outputting it to the dTimer div.

javascript setinterval

Here the variable nTimeID can be used so that the continuous process launched by the setInterval JavaScript can be stopped if necessary. The dTimer variable is a div in which the current time value is written every second.

Page Timer Logic

The usual second-by-second time calculation is optimal in most cases of programming a universal page timer via the setInterval JavaScript function. The given example shows the formation of the current time, the functionality for performing other actions is excluded from it.

In practice, having such a universal code, it can be supplemented with variables that vary at other time intervals for which specific actions are assigned.

javascript setinterval stop

In particular, if you write a site that works with the electronic currency exchange, you will need to do:

  • Exchange survey on the latest exchange reports (every 2 minutes);
  • checking the positions of client's requests for exchange (every 1 minute);
  • evaluate the dynamics of rates for each currency (every 5 minutes, per hour, per day).

Polling options and interval parameters depend on the specific exchange and customer ideas, but the JavaScript setInterval function for reliability and stability will satisfy the most demanding condition.


All Articles