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

Choosing Business Software: A Step-by-Step Comparison Guide

Every workplace runs on apps today, but choosing software that actually delivers has turned into a painful guessing game. Slick product sites promise the moon, yet companies…

Read More

Hyderabad Events This Weekend: Where to Go and What to Do

Hyderabad is always buzzing. Something fun is always happening somewhere. But here’s the problem. You often don’t know where. Your weekend slips away in confusion. Should you…

Read More

Simple Ways to Choose Modern Analytics Tools That Never Break

Introduction Most data teams have lived through this headache. A vendor shows off shiny charts, slick buttons, and fast dashboards during an afternoon sales demo. Your company…

Read More

Accredited Hospitals and Top Doctors for Hip Replacement Surgery Abroad

Hip pain steals your joy. It makes simple things hurt. Walking to the car hurts. Sleeping at night hurts too. When your hip wears down, getting help…

Read More

A Beginner’s Guide to Multi-Cloud Monitoring Tools for DataOps Teams

A retail company stores customer data in AWS. It runs analytics in Google Cloud. It builds reports in Azure. One morning, the reporting dashboard shows old numbers….

Read More

Online Tutors India: A Guide to Choosing the Right Teacher

If you want to find tutors online, you are not alone. Thousands of students search for tutors, coaching classes, and colleges every day. But too many options…

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