डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

डिप्लोमा इन ऑफिस मैनेजमेंट एण्ड अकाउटिंग

Full Stack Web Development with Laravel

Full Stack Web Development with Laravel

Affiliate Program

Affiliate Program

Javascript setInterval counter example

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>setInterval counter</title>
</head>
<body>

	<h1 id="timer"></h1>

	<button type="button" onclick="startInterval();">Start interval</button>

	<script type="text/javascript">
		"use strict"
		let n=60; // in seconds
		let i;

		function startInterval(){
			i=setInterval(()=>{
				document.getElementById("timer").innerHTML=n;
				if(n==0){
					clearInterval(i);
					// switch state for states
				}
				n=n-1;
			},100);
		}		
	</script>	
</body>
</html>
© 2016 - 2023, All Rights are Reserved.