“Where is the source code for the setTimeout and setInterval functions? Where would you look for it? You can not google :) "
setTimeout
and setInterval
, proudly referred to as "JavaScript Timers", are not included in any ECMAScript specification or JavaScript engine implementation. Timer functions are implemented at the browser level, so their implementation differs in different browsers. Also, timers are natively implemented in the Node.js runtime itself.Window
interface, which is also associated with some other functions and objects. This interface provides global access to all its elements in the main JavaScript scope. That is why the setTimeout
function can be performed directly in the browser console.global
object, which is designed like a Window
browser interface. The source code for the timers in Node is shown here . // example1.js setTimeout( () => { console.log('Hello after 4 seconds'); }, 4 * 1000 );
setTimeout
output of the welcome message is delayed by 4 seconds. The second argument to setTimeout
is the delay (in ms). I multiply 4 by 1000 to get 4 seconds.setTimeout
is a function whose execution will be postponed.example1.js
with the node command, Node pauses for 4 seconds and then displays a welcome message (followed by an exit).setTimeout
is just a reference to a function . It should not be a built-in function, such as example1.js
. Here is the same example without using the built-in function: const func = () => { console.log('Hello after 4 seconds'); }; setTimeout(func, 4 * 1000);
setTimeout
is used for delay takes any arguments, then the remaining arguments of the setTimeout
function itself (after the 2 that we have already studied) can be used to transfer the values of the arguments to the deferred function. // : func(arg1, arg2, arg3, ...) // : setTimeout(func, delay, arg1, arg2, arg3, ...)
// example2.js const rocks = who => { console.log(who + ' rocks'); }; setTimeout(rocks, 2 * 1000, 'Node.js');
rocks
function, deferred for 2 seconds, takes a who
argument, and the call to setTimeout
passes the value “Node.js” to it as such a who
argument.example2.js
with the node
command, the phrase “Node.js rocks” will be displayed in 2 seconds.setTimeout
, we will display 2 following messages after the corresponding delays.setTimeout
calls will need to use the same function. // solution1.js const theOneFunc = delay => { console.log('Hello after ' + delay + ' seconds'); }; setTimeout(theOneFunc, 4 * 1000, 4); setTimeout(theOneFunc, 8 * 1000, 8);
theOneFunc
receives the delay
argument and uses the value of the given delay
argument in the message displayed on the screen. Thus, the function can display different messages depending on the delay value we will tell it.theOneFunc
in two calls to setTimeout
, and the first call works after 4 seconds, and the second after 8 seconds. Both of these calls to setTimeout
also get the 3rd argument, representing the delay
argument for theOneFunc
.solution1.js
with the node command, we will display the requirements of the task, and the first message will appear after 4 seconds, and the second after 8 seconds.setTimeout
in a loop, but the timer API also offers the setInterval
function, with which you can program the "eternal" execution of an operation.setInterval
: // example3.js setInterval( () => console.log('Hello every 3 seconds'), 3000 );
example3.js
with the node
command, Node will output this command until you forcibly terminate the process (CTRL + C).setTimeout
returns a timer ID, and you can use this timer ID when calling clearTimeout
to cancel the timer. Here is an example: // example4.js const timerId = setTimeout( () => console.log('You will not see this one!'), 0 ); clearTimeout(timerId);
timerId
value and immediately cancel this timer by calling clearTimeout
.example4.js
with the node
command, Node prints nothing - the process will simply end immediately.setTimeout
with a value of 0 ms. The Node.js setImmediate
API has another function called setImmediate
, and it basically does the same thing as setTimeout
with a value of 0 ms, but in this case, the delay can be omitted: setImmediate( () => console.log('I am equivalent to setTimeout with 0 ms'), );
setImmediate
function is setImmediate
supported in all browsers . Do not use it in the client code.clearTimeout
there is a clearInterval
function that does the same thing, but with setInerval
calls, and there is also a clearImmediate
call.setTimeout
after 0 ms, this operation does not occur immediately (after setTimeout
), but only after all the script code has been completely executed (including the clearTimeout
call)?setTimeout
, which should work out in half a second - but this does not happen: // example5.js setTimeout( () => console.log('Hello after 0.5 seconds. MAYBE!'), 500, ); for (let i = 0; i < 1e10; i++) { // }
for
loop. The value of 1e10
is 1 with 10 zeros, so the cycle lasts 10 billion processor cycles (in principle, this is how an overloaded processor is simulated). Node can do nothing until this loop ends.setTimeout
delay is not a guaranteed, but rather a minimum value . A value of 500 ms means that the delay will last at least 500 ms. In fact, the script will take much more time to display the welcome line on the screen. First, he will have to wait until the blocking cycle is completed.setTimeout
cannot be called. let counter = 0; const intervalId = setInterval(() => { console.log('Hello World'); counter += 1; if (counter === 5) { console.log('Done'); clearInterval(intervalId); } }, 1000);
counter
, and then called setInterval
, which takes its id.intervalId
constant. The delay interval is 1000 ms.this
inside a regular function, like this: function whoCalledMe() { console.log('Caller is', this); }
this
will match the caller . If you define the above function inside the Node REPL, then the global
object will call it. If you define a function in the browser console, then the window
object will call it. const obj = { id: '42', whoCalledMe() { console.log('Caller is', this); } }; // : obj.whoCallMe
obj.whoCallMe
function we will directly use the link to it, the obj
object (identified by its id
) will act as the caller:obj.whoCallMe
to the obj.whoCallMe
call? // ?? setTimeout(obj.whoCalledMe, 0);
Timeout
object:this
used inside normal functions. When using switch functions, the caller should not bother you at all.Hello World. 1
Hello World. 2
Hello World. 3
...
setInterval
here, but you can manually adjust the interval execution using setTimeout
inside a recursive call. The first function executed with setTimeout
will create the next timer, and so on.let
/ var
, we cannot have a counter to increment the delay on each recursive call; instead, you can use the arguments of the recursive function to increment during a recursive call. const greeting = delay => setTimeout(() => { console.log('Hello World. ' + delay); greeting(delay + 1); }, delay * 1000); greeting(1);
Hello World. 100 // 100
Hello World. 100 // 200
Hello World. 100 // 300
Hello World. 100 // 400
Hello World. 100 // 500
Hello World. 200 // 700
Hello World. 200 // 900
Hello World. 200 // 1100
...
setInterval
calls (and not setTimeout
) and only ONE if
.setInterval
calls, here we will need to use recursion, as well as increase the delay of the next setInterval
call. In addition, the if
will need us to make this happen only after 5 calls to this recursive function. let lastIntervalId, counter = 5; const greeting = delay => { if (counter === 5) { clearInterval(lastIntervalId); lastIntervalId = setInterval(() => { console.log('Hello World. ', delay); greeting(delay + 100); }, delay); counter = 0; } counter += 1; }; greeting(100);
Source: https://habr.com/ru/post/426709/
All Articles