Difference Between setTimeout & setInterval in JavaScript Last Updated : 26 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report JavaScript has both setTimeout and setInterval functions that are used for executing code after a certain delay. However, they differ in how they handle the timing of execution. Understanding their differences is crucial for effectively managing asynchronous operations in our code which is explained below: Table of Content Using setTimeoutUsing setIntervalDifference between setTimeout and setIntervalUsing setTimeoutsetTimeout is a function in JavaScript used to execute a piece of code or a function after a specified delay. It allows you to execute a function only once after a certain amount of time has passed. Syntax:setTimeout(function, delay);Example: Printing the output "Hello World!" after 2 seconds of code execution. JavaScript function greet() { console.log("Hello, world!"); } // Executes greet function after 2 seconds setTimeout(greet, 2000); Output: OutputUsing setIntervalsetInterval is another function in JavaScript used to execute a piece of code or a function repeatedly at a fixed interval. It allows you to execute a function repeatedly with a specified time delay between each execution. Syntax:setInterval(function, delay);Example: To demonstrate printing the time at interval of 1 seconds. JavaScript function displayTime() { console.log(new Date().toLocaleTimeString()); } // Executes displayTime function every second setInterval(displayTime, 1000); Output: OutputDifference between setTimeout and setIntervalFeature setTimeout setInterval Execution Executes function once after delay Executes function repeatedly Interval Executes function only once Repeatedly executes function Timing Control Delay specified for single execution Interval specified for repeated executions Usage Ideal for executing code once Ideal for executing code Clears clearTimeout() method can be used to cancel scheduled execution clearInterval() method can be used to stop further executions Example setTimeout(function, delay); setInterval(function, delay); Comment More infoAdvertise with us Next Article How to stop setInterval Call in JavaScript ? S strikeackr Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript setTimeout() & setInterval() Method JavaScript SetTimeout and SetInterval are the only native function in JavaScript that is used to run code asynchronously, it means allowing the function to be executed immediately, there is no need to wait for the current execution completion, it will be for further execution.JavaScriptsetTimeout(gf 2 min read Difference Between console.time and console.timeEnd in JavaScript In JavaScript, console.time and console.timeEnd are powerful tools used for performance measurement. These methods allow developers to measure the time a block of code takes to execute, which is particularly useful for optimizing performance and debugging.Table of ContentWhat is console. time?What i 2 min read How to stop setInterval Call in JavaScript ? In JavaScript, the setInterval() function is used to repeatedly execute a specified function at a fixed interval. However, there may be scenarios where we need to stop the execution of setInterval() calls dynamically. Stopping a setInterval() call in JavaScript is essential to prevent ongoing repeti 2 min read How to Execute setInterval Function without Delay for the First Time in JavaScript? When working with JavaScript, the setInterval function is a powerful tool for executing a block of code repeatedly at specified intervals. The setInterval() method always invokes the function after the delay for the first time. Approach 1: Calling the function once before executing setInterval:Invok 2 min read Difference between Anonymous and Named functions in JavaScript In JavaScript or in any programming language per say, functions, loops, mathematical operators and variables are the most widely used tools. This article will tell you about the difference between anonymous functions and named functions. We will discuss all the required concepts in this article to k 4 min read What is the Difference Between $evalAsync and $timeout in AngularJS? AngularJS is a JavaScript framework, which may be utilized by including it in an HTML web page the usage of a <script> tag. AngularJS enables in extension the HTML attributes with the assistance of directives and binding of data to the HTML with expressions. It provides various tools for handl 5 min read Like