JavaScripti taimerid: kõik, mida peate teadma
Mõni nädal tagasi tweetisin seda intervjuuküsimust:
*** Vastake oma küsimusele kohe enne jätkamist ***
Ligikaudu pool tweetile antud vastusest oli vale. Vastus EI OLE V8 (või mõni muu VM) !! Kuigi suurepäraselt tuntud kui "JavaScript Taimerid", funktsioonid nagu setTimeout
ja setInterval
ei ole osa ECMAScript näitajad või JavaScripti mootor rakendusi. Taimerifunktsioone rakendavad brauserid ja nende rakendused on erinevate brauserite vahel erinevad. Taimereid rakendab loomulikult ka Node.js käitusaeg ise.
Brauserites on taimeri peamised funktsioonid Window
liidese osa , millel on veel mõned funktsioonid ja objektid. See liides muudab kõik selle elemendid JavaScripti peamises ulatuses globaalselt kättesaadavaks. Sellepärast saate käivitada setTimeout
otse oma brauseri konsoolis.
Node'is on taimerid osa global
objektist, mis käitub sarnaselt brauseri Window
liidesega. Taimerite lähtekoodi näete Node'is siin.
Mõni võib arvata, et see on halb intervjuu küsimus - miks selle teadmine ikkagi on ?! JavaScripti arendajana arvan, et eeldatakse, et teate seda, sest kui te seda ei tea, võib see olla märk sellest, et te ei saa täielikult aru, kuidas V8 (ja muud virtuaalsed virtuaalseadmed) suhtlevad brauserite ja Node'iga.
Teeme mõned näited ja väljakutsed taimeri funktsioonide kohta, kas pole?
Värskendus: see artikkel on nüüd osa minu täielikust sissejuhatusest Node.js-i.Selle uuendatud versiooni saate lugeda siit.
Funktsiooni täitmisega viivitamine
Taimerifunktsioonid on kõrgema järgu funktsioonid, mida saab kasutada teiste funktsioonide täitmise viivitamiseks või kordamiseks (mille nad saavad esimese argumendina).
Siin on näide viivitamise kohta:
// example1.js setTimeout( () => { console.log('Hello after 4 seconds'); }, 4 * 1000 );
Selles näites kasutatakse setTimeout
tervitussõnumi printimise edasilükkamist 4 sekundi võrra. Teine argument setTimeout
on viivitus (ms). Seetõttu korrutasin 4 1000-ga, et saada sellest 4 sekundit.
Esimene argument setTimeout
funktsioonile on funktsioon, mille käivitamine viibib.
Kui käivitate example1.js
faili node
käsuga, peatub Node 4 sekundit ja seejärel prindib tervitusteate (ja väljub pärast seda).
Pange tähele, et esimene argument setTimeout
on lihtsalt funktsioon viide . See ei pea olema sisemine funktsioon nagu see, mis example1.js
on. Siin on sama näide, ilma sisemise funktsiooni kasutamata:
const func = () => { console.log('Hello after 4 seconds'); }; setTimeout(func, 4 * 1000);
Argumentide edastamine
Kui funktsioon, mis kasutab setTimeout
selle täitmise viivitamist, aktsepteerib mis tahes argumente, võime ülejäänud argumente setTimeout
enda jaoks kasutada (pärast kahte seni teada saanud) argumendi väärtuste edastamiseks viivitatud funktsioonile.
// For: func(arg1, arg2, arg3, ...) // We can use: setTimeout(func, delay, arg1, arg2, arg3, ...)
Siin on näide:
// example2.js const rocks = who => { console.log(who + ' rocks'); }; setTimeout(rocks, 2 * 1000, 'Node.js');
Ülaltoodud rocks
funktsioon, mis viibib 2 sekundit, aktsepteerib who
argumenti ja setTimeout
kõne edastab selle argumendina väärtuse “ Node.js ” who
.
Käsu example2.js
koos node
käsk välja printida " Node.js kivimid " 2 sekundi pärast.
Taimerite väljakutse nr 1
Kasutades seni õpitud teavet setTimeout
, printige järgmised 2 teadet pärast nende vastavaid viivitusi.
- 4 sekundi pärast printige sõnum “ Tere pärast 4 sekundit ”
- Printige 8 sekundi pärast teade “ Tere pärast 8 sekundit ”.
Piirangud :
Oma lahenduses saate määratleda ainult ühe funktsiooni, mis sisaldab ka siseseid funktsioone. See tähendab, et paljud setTimeout
kõned peavad kasutama täpselt sama funktsiooni.
Lahendus
Selle väljakutse lahendan järgmiselt:
// solution1.js const theOneFunc = delay => { console.log('Hello after ' + delay + ' seconds'); }; setTimeout(theOneFunc, 4 * 1000, 4); setTimeout(theOneFunc, 8 * 1000, 8);
Olen pannud theOneFunc
vastu võtma delay
argumendi ja kasutanud selle delay
argumendi väärtust prinditud sõnumis. Nii saab funktsioon printida erinevaid teateid sõltuvalt viivituse väärtusest, mille me talle edastame.
Seejärel kasutasin theOneFunc
kahes setTimeout
kõnes: ühte, mis käivitub 4 sekundi pärast, ja teist, mis käivitub 8 sekundi pärast. Mõlemad setTimeout
kõned saavad ka kolmanda argumendi, mis esindab delay
argumenti theOneFunc
.
solution1.js
Faili käivitamine node
käsuga prindib väljakutse nõuded, esimene sõnum 4 sekundi pärast ja teine sõnum 8 sekundi pärast.
Funktsiooni täitmise kordamine
Mis oleks, kui paluksin teil igaveseks iga nelja sekundi tagant sõnumi printida?
Ehkki saate setTimeout
tsükli panna , pakub taimeri API ka setInterval
funktsiooni, mis täidaks nõude midagi igaveseks teha.
Siin on näide setIntervalist:
// example3.js setInterval( () => console.log('Hello every 3 seconds'), 3000 );
See näide prindib sõnumi iga 3 sekundi järel. Käsu example3.js
koos node
käsk teeb Sõlme printida seda sõnumit igavesti, kuni sa tappa protsessi (koos CTRL + C ).
Taimerite tühistamine
Kuna taimeri funktsiooni kutsumine ajastab toimingu, saab selle toimingu tühistada ka enne selle käivitamist.
Kõne setTimeout
taimeriga „ID” tagastamiseks saate selle taimeri ID koos clearTimeout
kõnega selle taimeri tühistamiseks kasutada. Siin on näide:
// example4.js const timerId = setTimeout( () => console.log('You will not see this one!'), 0 ); clearTimeout(timerId);
See lihtne taimer peaks käivituma pärast 0
ms-d (muutes selle kohe), kuid mitte, sest me hõivame timerId
väärtuse ja tühistame selle kohe pärast clearTimeout
kõnet.
Kui me ellu example4.js
koos node
käsuga, Sõlme ei prindi midagi ja protsess lihtsalt väljuda.
By the way, in Node.js, there is another way to do setTimeout
with 0
ms. The Node.js timer API has another function called setImmediate
, and it’s basically the same thing as a setTimeout
with a 0
ms but we don’t have to specify a delay there:
setImmediate( () => console.log('I am equivalent to setTimeout with 0 ms'), );
The setImmediate
function is not available in all browsers. Don’t use it for front-end code.
Just like clearTimeout
, there is also a clearInterval
function, which does the same thing but for setInerval
calls, and there is also a clearImmediate
call as well.
A timer delay is not a guaranteed thing
In the previous example, did you notice how executing something with setTimeout
after 0
ms did not mean execute it right away (after the setTimeout line), but rather execute it right away after everything else in the script (including the clearTimeout call)?
Let me make this point clear with an example. Here’s a simple setTimeout
call that should fire after half a second, but it won’t:
// example5.js setTimeout( () => console.log('Hello after 0.5 seconds. MAYBE!'), 500, ); for (let i = 0; i < 1e10; i++) { // Block Things Synchronously }
Right after defining the timer in this example, we block the runtime synchronously with a big for
loop. The 1e10
is 1
with 10
zeros in front of it, so the loop is a 10
Billion ticks loop (which basically simulates a busy CPU). Node can do nothing while this loop is ticking.
This of course is a very bad thing to do in practice, but it’ll help you here to understand that setTimeout
delay is not a guaranteed thing, but rather a minimum thing. The 500
ms means a minimum delay of 500
ms. In reality, the script will take a lot longer to print its greeting line. It will have to wait on the blocking loop to finish first.
Timers Challenge #2
Write a script to print the message “Hello World” every second, but only 5 times. After 5 times, the script should print the message “Done” and let the Node process exit.
Constraints: You cannot use a setTimeout
call for this challenge.
Hint: You need a counter.
Solution
Here’s how I’d solve this one:
let counter = 0; const intervalId = setInterval(() => { console.log('Hello World'); counter += 1; if (counter === 5) { console.log('Done'); clearInterval(intervalId); } }, 1000);
I initiated a counter
value as 0
and then started a setInterval
call capturing its id.
The delayed function will print the message and increment the counter each time. Inside the delayed function, an if
statement will check if we’re at 5
times by now. If so, it’ll print “Done” and clear the interval using the captured intervalId
constant. The interval delay is 1000
ms.
Who exactly “calls” the delayed functions?
When you use the JavaScript this
keyword inside a regular function, like this:
function whoCalledMe() { console.log('Caller is', this); }
The value inside the this
keyword will represent the caller of the function. If you define the function above inside a Node REPL, the caller will be the global
object. If you define the function inside a browser’s console, the caller will be the window
object.
Let’s define the function as a property on an object to make this a bit more clear:
const obj = { id: '42', whoCalledMe() { console.log('Caller is', this); } }; // The function reference is now: obj.whoCallMe
Now when you call the obj.whoCallMe
function using its reference directly, the caller will be the obj
object (identified by its id):

Now, the question is, what would the caller be if we pass the reference of obj.whoCallMe
to a setTimetout
call?
// What will this print?? setTimeout(obj.whoCalledMe, 0);
Who will the caller be in that case?
The answer is different based on where the timer function is executed. You simply can’t depend on who the caller is in that case. You lose control of the caller because the timer implementation will be the one invoking your function now. If you test it in a Node REPL, you’d get a Timetout
object as the caller:

Note that this only matters if you’re using JavaScript’s this
keyword inside regular functions. You don’t need to worry about the caller at all if you’re using arrow functions.
Timers Challenge #3
Write a script to continuously print the message “Hello World” with varying delays. Start with a delay of 1 second and then increment the delay by 1 second each time. The second time will have a delay of 2 seconds. The third time will have a delay of 3 seconds, and so on.
Include the delay in the printed message. Expected output looks like:
Hello World. 1 Hello World. 2 Hello World. 3 ...
Constraints: You can only use const
to define variables. You can’t use let
or var
.
Solution
Because the delay amount is a variable in this challenge, we can’t use setInterval
here, but we can manually create an interval execution using setTimeout
within a recursive call. The first executed function with setTimeout will create another timer, and so on.
Also, because we can’t use let/var, we can’t have a counter to increment the delay in each recursive call, but we can instead use the recursive function arguments to increment during the recursive call.
Here’s one possible way to solve this challenge:
const greeting = delay => setTimeout(() => { console.log('Hello World. ' + delay); greeting(delay + 1); }, delay * 1000); greeting(1);
Timers Challenge #4
Write a script to continuously print the message “Hello World” with the same varying delays concept as challenge #3, but this time, in groups of 5 messages per main-delay interval. Starting with a delay of 100ms for the first 5 messages, then a delay of 200ms for the next 5 messages, then 300ms, and so on.
Here’s how the script should behave:
- At the 100ms point, the script will start printing “Hello World” and do that 5 times with an interval of 100ms. The 1st message will appear at 100ms, 2nd message at 200ms, and so on.
- After the first 5 messages, the script should increment the main delay to 200ms. So 6th message will be printed at 500ms + 200ms (700ms), 7th message will be printed at 900ms, 8th message will be printed at 1100ms, and so on.
- After 10 messages, the script should increment the main delay to 300ms. So the 11th message should be printed at 500ms + 1000ms + 300ms (18000ms). The 12th message should be printed at 21000ms, and so on.
- Continue the pattern forever.
Include the delay in the printed message. The expected output looks like this (without the comments):
Hello World. 100 // At 100ms Hello World. 100 // At 200ms Hello World. 100 // At 300ms Hello World. 100 // At 400ms Hello World. 100 // At 500ms Hello World. 200 // At 700ms Hello World. 200 // At 900ms Hello World. 200 // At 1100ms ...
Constraints: You can use only setInterval
calls (not setTimeout
) and you can use only ONE if statement.
Solution
Because we can only use setInterval
calls, we’ll need recursion here as well to increment the delay of the next setInterval
call. In addition, we need an if statement to control doing that only after 5 calls of that recursive function.
Here’s one possible solution:
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);
Thanks for reading.
Kui hakkate alles õppima Node.js-i, avaldasin hiljuti Pluralsightis esimese sammu kursuse , vaadake seda:
