Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
JavaScript setTimeout() & setInterval() Method
Next article icon

JavaScript setTimeout() & setInterval() Method

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

JavaScript
setTimeout(gfg1, 2000); function gfg1() {     console.log("gfg1"); } function gfg() {     console.log("gfg"); } setInterval(gfg, 1000); 

Output:

gfg gfg1 gfg ......

Key points:

  • gfg1 will be called once as setTimeout is set for 2 seconds
  • gfg function will called each second as it passed through the setInterval.

JavaScript setTimeout() Method

The setTimeout() Method executes a function, after waiting a specified number of milliseconds. 

JavaScript
setTimeout(gfg1, 2000); function gfg1() {     console.log("gfg1"); } 

Output:

gfg1

JavaScript setInterval() Method

The setInterval() method repeats a given function at every given time interval. 

JavaScript
function gfg() {     console.log("gfg"); } setInterval(gfg, 1000); 

Output: After every second a new "gfg" message will be displayed.

Interesting Facts

  • Asynchronous Execution: Both methods are asynchronous, meaning the browser doesn’t block other code execution while waiting for the timer.
  • Return Value: Both methods return a unique identifier (ID), which can be used with clearTimeout() or clearInterval() to stop the scheduled task.
  • Timer Accuracy: Timers are not perfectly precise; delays can vary due to browser limitations and other queued tasks.
  • Infinite Intervals: Using setInterval() without a clearInterval() call can lead to infinite loops, potentially causing performance issues.
  • Nested Timers: A setTimeout() can mimic a setInterval() by recursively calling itself after each execution.
  • Minimum Delay: The minimum delay for setTimeout() or setInterval() is 4 milliseconds, though it may increase in inactive browser tabs for performance reasons.
  • Timer in Node.js: Both methods are also available in Node.js with similar behavior but are part of the global object.

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Next Article
JavaScript setTimeout() & setInterval() Method
https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
  • Misc
  • JavaScript
  • Web Technologies
  • javascript-functions
Practice Tags :
  • Misc

Similar Reads

    JavaScript setInterval() Method
    The setInterval() method calls a function at specified intervals (in milliseconds). It continues calling the function until clearInterval() is called or the window is closed. This method is useful for tasks that need periodic execution, like updating animations or refreshing data. Important Note: se
    2 min read
    JavaScript setTimeout() Method
    JavaScript setTimeout() method allows you to schedule the execution of a function or the evaluation of a code after a specified delay. The setTimeout() method calls a function after several milliseconds. setTimeout() is for executing a function once after a specified delay. Syntax:setTimeout(functio
    2 min read
    JavaScript Date setMinutes() Method
    JavaScript date.setMinutes() method is used to set minutes into a Date object which is created using the Date() constructor. Syntax: DateObj.setMinutes(Minutes_Value) Parameter: This method accepts a single parameter as mentioned above and described below: minutes_Value: This parameter holds the val
    3 min read
    JavaScript Program to Pass Parameter to a setTimeout() Method
    In this article, we will discuss how to pass parameters to the setTimeout() method in JavaScript. The setTimeout() method is used to delay the execution of a piece of code. This method executes a function, after waiting a specified number of milliseconds. We have different approaches to passing para
    3 min read
    JavaScript Date setUTCMinutes() Method
    The date.setUTCMinutes() method is used to set minutes according to universal time into a date object which is created using the Date() constructor. Syntax: DateObj.setUTCMinutes(Minutes_Value); Parameter: This method accepts a single parameter as mentioned above and described below: minutes_Value:
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences