ItSolutionStuff.com

How to Stop Setinterval() After Sometimes in JQuery?

By Hardik Savani • June 17, 2023
jQuery

Hello Friends,

This detailed guide will cover jquery setinterval stop after sometime. This tutorial will give you a simple example of setinterval jquery stop timer example. you will learn javascript setinterval stops after a while. In this article, we will implement a javascript setinterval stop itself. So, let's follow a few steps to create an example of how to stop setinterval after some time in jquery.

Sometimes we require to stop setInterval() after a while few time, so today i am going to give you simple example with setinterval stops after a while.

In this example i used two function as listed bellow:

setInterval() : Call on specified intervals time.

clearInterval() : Stop set interval.

You can see bellow full example, how to stop setinterval after 1 minute, You can also check demo, how work this example:

index.html

<!DOCTYPE html>

<html>

<head>

<title>Jquery setinterval stop after sometime</title>

<script type="text/javascript" src="//code.jquery.com/jquery-1.4.2.min.js"></script>

</head>

<body>

<button>Click to Start</button>

<pre></pre>

<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>


</body>

</html>

Maybe it can help you...

Tags: jQuery
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check If Checkbox is Checked or Not in JQuery?

Read Now →

JQuery Open Link in New Tab on Click Example

Read Now →

JQuery Remove Multiple Values from Array Example

Read Now →

How to Remove Element from Array in JQuery?

Read Now →

JQuery Redirect to Another Page After 5 Seconds Example

Read Now →

JQuery Rotate Image Animation Example Code

Read Now →

How to Remove Empty or Null Values from Array in JQuery?

Read Now →

PHP JQuery Chosen Ajax Autocomplete Example

Read Now →

How to Remove All Spaces from String in JQuery?

Read Now →

How to Remove Comma from String in JQuery?

Read Now →

JQuery Select Box with Search using Select2 JS Example

Read Now →

Bootstrap Fancy Alert Box using SweetAlert Example

Read Now →

JQuery Notification Popup Box using Toastr JS Example

Read Now →

JQuery Tag Input Plugin Example Code

Read Now →