How To Stop setinterval() After Sometimes using JQuery?

20-Apr-2021

.

Admin

How To Stop setinterval() After Sometimes using JQuery?

Hi Guys,

In this blog, I will show you how to stop setinterval() after sometimes using jquery. We will learn jquery stop setinterval() after sometimes. This article will give simple and way to stop setinterval() after sometimes using 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 will use two function as bellow:

setInterval() : Call on specified intervals time.

clearInterval() : Stop set interval.

Here I will give you full example for how to stop setinterval() after sometimes in jquery So let's see the bellow example:

Example:

<!DOCTYPE html>

<html>

<head>

<title>Jquery setinterval stop after sometime - NiceSnippets.com</title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.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>

It will help you....

#Jquery