How To Use setInterval() Method Using JavaScript?


Share:
Published By: Admin, Published On: Nov 19, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use setInterval() method example.We will check how to use setInterval() method. This is a short guide on use setInterval() method in javascript. Here you will learn how to use javascript setInterval() method. Let's get started with how to use setInterval() method in javascript.

Here i will give you many example how to use setInterval() method using javascript.

Example : 1
<html>  
    <head>  
        <title>How To Use setInterval() Method Using JavaScript? - MyWebtuts.com</title>  
    </head>  
    <body>  
        <h3>How To Use setInterval() Method Using JavaScript? - MyWebtuts.com</h3>  
        <p>Here, an alert dialog box displays on every three seconds.</p>    
        <script>  
            var a1;  
            a1 = setInterval(tst, 3000);     
            function tst() {  
                alert(" Welcome to the MyWebtuts.com ");  
            }
        </script>    
    </body>  
</html>   
Output :
Here, an alert dialog box displays on every five seconds.
Welcome to the MyWebtuts.com(After, Every five second)
Welcome to the MyWebtuts.com(After, Every five second)
Example : 2
<html>
	<head>
		<title>How To Use setInterval() Method Using JavaScript? - MyWebtuts.com</title>
	</head>
	<body>
		<h1>How To Use setInterval() Method Using JavaScript? - MyWebtuts.com</h1>
		<h3> This is an example of using the setInterval() method </h3>
		<p> Here, the background color changes on every 5000 milliseconds. </p>

		<script>
			var a1 = setInterval(clr, 5000);

			function clr() {
				var a2 = document.body;
				a2.style.backgroundColor = a2.style.backgroundColor == "lightblue" ? "lightgreen" : "lightblue";
			}
		</script>
	</body>
</html>  
Output :
 Here, the background color changes on every 5000 milliseconds.
 Lightblue (After, Every five second)
 Lightgreen (After, Every five second)

It will help you...

Share: