A few weeks ago, I tweeted this interview question: *** Answer the question in your head now before you proceed *** About half the replies to the Tweet were wrong. The answer is NOT V8 (or other VMs)!! While famously known as “JavaScript Timers”, functions like In browsers, the main timer functions are part of the In Node, timers are part of the Some might think this is a bad interview question — why does knowing this matter anyway?! As a JavaScript developer, I think you’re expected to know this because if you don’t, that might be a sign that you don’t completely understand how V8 (and other VMs) interacts with browsers and Node. Let’s do a few examples and challenges about timer functions, shall we? Timer functions are higher-order functions that can be used to delay or repeat the
execution of other functions (which they receive as their first argument). Here’s an example about delaying: This example uses The first argument to If you execute the Note that the first argument to
Passing ArgumentsIf the function that uses
Here’s an example:
The Executing Timers Challenge #1Using what you learned
so far about
Constraints: SolutionHere’s how I’d solve this challenge:
I’ve made I then used Executing
the Repeating the execution of a functionWhat if I asked you to print a message every 4 seconds, forever? While you can put Here’s an example of setInterval:
This example will print its message every 3 seconds. Executing Cancelling TimersBecause calling a timer function schedules an action, that action can also be cancelled before it gets executed. A call to
This simple timer is supposed to fire after When we execute By the way, in Node.js, there is another way to do
The Just like A timer delay is not a guaranteed thingIn the previous example, did you notice how executing something with Let me make this point clear with an example. Here’s a simple
Right after defining the timer in this example, we block the runtime synchronously with a big This of course is a very bad thing to do in practice, but it’ll help you here to understand that Timers Challenge #2Write 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 SolutionHere’s how I’d solve this one:
I initiated a The delayed function will print the message and increment the counter each time. Inside the delayed function, an Who exactly “calls” the delayed functions?When you use the JavaScript
The value inside the Let’s define the function as a property on an object to make this a bit more clear:
Now when you call the Now, the
question is, what would the caller be if we pass the reference of
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 Note that this only matters if you’re using JavaScript’s Timers Challenge #3Write 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:
Constraints: You can only use SolutionBecause the delay amount is a variable in this challenge, we can’t use
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:
Timers Challenge #4Write 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:
Include the delay in the printed message. The expected output looks like this (without the comments):
Constraints: You can use only SolutionBecause we can only use Here’s one possible solution:
Thanks for reading. If you’re just beginning to learn Node.js, I recently published a first-steps course at Pluralsight, check it out: https://jscomplete.com/c/nodejs-getting-startedLearn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you stop a timer in JavaScript?To cancel a setTimeout() method from running, you need to use the clearTimeout() method, passing the ID value returned when you call the setTimeout() method.
How do you stop a function in JavaScript?To stop the execution of a function in JavaScript, use the clearTimeout() method. This function call clears any timer set by the setTimeout() functions.
How do timers work in JavaScript?In JavaScript, a timer is created to execute a task or any function at a particular time. Basically, the timer is used to delay the execution of the program or to execute the JavaScript code in a regular time interval. With the help of timer, we can delay the execution of the code.
How do you stop a setInterval function?Answer: Use the clearInterval() Method
The setInterval() method returns an interval ID which uniquely identifies the interval. You can pass this interval ID to the global clearInterval() method to cancel or stop setInterval() call.
|