Usually, a piece of JavaScript code is executed synchronously, this means the code runs line after line as it’s written after hoisting in one thread. However, there might be situations when you need to run a block of your code at some point later in the future – when you need to delay the execution of some instructions. This concept of telling your code to work asynchronously is called ‘scheduling a call’ – as simple as that. There are two methods in JavaScript that enable to schedule a call – (nested) setTimeout() and setInterval(). A very simple explanation would say setTimeout() runs the code once after the timeout – it fires once or it runs the call only once after an interval, while setInterval() runs the code repeatedly, with the length of the timeout between each interval – it repeats the call, it fires again and again in intervals.
In this tutorial, you’ll learn how these two methods work, what is the difference between (nested) setTimeout and setInterval and we’ll give you some practical examples to understand these concepts a bit better.
…