How to Stop Setinterval() After Sometimes in JQuery

Introduction

Have you ever found yourself in a situation where you needed to stop the setInterval() function in jQuery after a certain period of time? If so, you’re not alone. This common problem can be a bit tricky to solve, but fear not! In this article, we’ll explore different approaches to tackle this issue and find the best solution for your needs.

Solution 1: Using setInterval()

<!DOCTYPE html>
<html>
<head>
    <title>Jquery setinterval stop after sometime</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("button").click(function(){
  
                 $(this).attr("disabled",true);
                 $("pre").append('Stop After 1min.<br/>');
  
                 var countTime = 0;
                 var storeTimeInterval = setInterval(function(){
                    ++countTime;
                    $("pre").append(countTime*5 + 'sec.<br/>');
                    if(countTime == 12){
                        clearInterval(storeTimeInterval);
                        $("pre").append('stop');
                    }
                    
                 }, 5000);
  
            });
        });
    </script>
</head>
<body>
   
    <button>Click to Start</button>
    <pre></pre>
  

</body>
</html>

Output:-

Hopefully, it can help you…!!!

Related Posts

Discover How Tool Selection Impacts DataOps Success and Team Productivity

Modern businesses run on data. Every click, sale, and customer interaction generates information that companies use to make decisions. However, raw data is like crude oil; it…

Read More

Strategies to Monitor Multi-Cloud Data Pipelines and Prevent Failures

Introduction Today, data drives almost every major business decision. To keep up with massive amounts of information, organizations no longer rely on just one cloud provider. Instead,…

Read More

Improving Enterprise Data Governance Strategy Through Modern DataOps Platform Tools

Introduction Modern organizations run on data. Every transaction, customer click, and supply chain adjustment generates valuable information. However, managing this information at scale presents significant challenges. If…

Read More

Streamlining Enterprise DataOps Strategies With AI-Based Analytics Tools Safely

Introduction Data has become the lifeblood of modern businesses. However, managing vast amounts of information can quickly overwhelm traditional data teams. DataOps—the practice of bringing agility, continuous…

Read More

Navigating Your Ultimate Goa Itinerary: Best Places to Visit Safely

Introduction Planning a trip to India’s most iconic coastal paradise can feel overwhelming, but finding the absolute best places to visit in Goa doesn’t have to be…

Read More

The Ultimate Checklist for Planning Cosmetic Surgery Abroad Safely

Introduction The quest for self-improvement and aesthetic refinement has evolved from a localized luxury into a highly accessible global phenomenon. Today, patients seeking transformative treatments are no…

Read More
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x