
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to make a setInterval() stop after some time or after a number of actions in JavaScript?
Use some conditions to stop after some time.
The below code will stop in half a minute.
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <script> var now = new Date().getTime(); var interval = setInterval(function () { if (new Date().getTime() - now > 30000) { clearInterval(interval); return; } console.log("working"); }, 2000); </script> </html>
To run the above program, save the file name anyName.html(index.html) and right click on the file. Select the option “Open with live server” in VS Code editor.
Output
This will produce the following output −
- Related Articles
- How to hide a widget after some time in Python Tkinter?
- How to stop Tkinter after function?
- A balloon when kept in sun, bursts after some time. Why?
- How can we create a MySQL one-time event that executes after some specified time interval?
- How to redirect website after certain amount of time without JavaScript?
- How to make my textfield empty after button click in JavaScript?
- Why does a moving ball stop after sometime?
- How to Auto-Number or Renumber after Filter in Excel?
- How to optimize a MySQL table after deleting some rows?
- How to make a jQuery function call after “X” seconds?
- Ideal Time to Eat — Before or After Working Out?
- How to Change the Time Interval of setinterval() Method at RunTime using JavaScript ?
- How to get a part of string after a specified character in JavaScript?
- How to execute a task repeatedly after fixed time intervals in iOS
- How can we create a MySQL recurring event that executes after a specified time period and ends after a specified time period?

Advertisements