Are JavaScript timers coming from ECMAScript or HTML?

This focus of this video is to answer these questions: 1) Where Javascript timers are coming from? 2) Is setTimeout() and setInterval() part of ECMAScript or is it an HTML Standard ?

Here is the HTML spec for Timers

Google Chrome Timers inside Blink codebase

Mozilla timers inside Gecko code

Safari timers inside Webkit codebase

Node.js documentation for timers is available here

Node.js implementation of timers

Full Transcript

00:00 Hi there. Welcome to JavaScript series and I'm Deeksha Sharma from bonsaiilabs. Today, we are going to see from where exactly these so-called JavaScript timers coming from ? Is setTimeout() and setInterval() part of ECMAScript or is it an HTML standard? Let's dive into see what's the truth. setTimeout() and setInterval() are methods available to us in browser and also in Node.js runtime. They provide a way to schedule a callback after a period of time. clearTimeout() and clearInterval() cancels the timeout and time interval that were previously established by setTimeout()and setInterval() methods. But the question is where these timers implementations are coming from ? Are they coming from ECMAScript and JavaScript engines implement them or they're part of HTML standard spec where browsers own the responsibility of implementing them? The answer is they're not part of ECMAScript.

00:54 They're part of HTML standard specification that laid down the stepwise algorithms that browsers should use to implement these timer methods. Here's the HTML 5 specification from W3C that describe the steps indicating how setTimeout()and setInterval() methods should behave. Also, if you are curious, you may want to check out the ECMAScript specification which does not deal with timers. I've added the link for both of these documentations for you to have a look at. Now comes the main part. How do browsers implement these timer methods? Chrome, Mozilla and Safari use their own rendering engines called blink, gecko and WebKit. These rendering engines do a lot of work to bring the contents of a webpage on the screen that we see. One of the responsibilities of a rendering engine is to implement the spec of the web platform, which includes HTML standard, DOM, CSS and WebIDL. So in short it's the rendering engine sitting in the browser code that implement timers and not the JavaScript engines.

01:58 All of the rendering engines are open source, so I've added the GitHub links to the classes that implements the timers. Feel free to check them out. I believe the next question in your head would be, if timers a part of HTML spec, then how can I use setTimeout() and setInterval() when running the server side code using Node.js? This is because Node.js provide its own timer functions that implement a pretty similar API, just like you see in web browsers. So the timers are not part of, V8 JavaScript engine embedded by Node.js. They're internal implementation by Node.js itself. Again, I've added the link to the official documentation and github class for timers in Node.js code base. You may want to give it a read. All right! if you found this video useful, be sure to subscribe and until then keep learning and share your knowledge. I'll see you in the next video.