setTimeout and clearTimeout :
---------------------------------------
html file :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="main-div">
<div>
<p>Want to know my name</p>
<p id="showmyname"></p>
<br>
<button id="btn">Click here</button>
<br>
<button id="btn1"> Stop</button>
</div>
</div>
<script src=" prac.js"></script>
</body>
</html>
javascript file :
let myname = document.getElementById("showmyname");
let btn = document.getElementById("btn");
let btn1 = document.getElementById("btn1");
let timeout;
btn.addEventListener('click', () => {
myname.innerHTML = "loading....."
timeout = setTimeout(() => {
myname.innerHTML = "himanshu shekhar"
}, 5000)
})
btn1.addEventListener('click',()=>{
myname.innerHTML = "stopped!"
clearTimeout(timeout)
})
clearInterval() and clearTimeout() :
---------------------------------------------
HTML file is same as above
java script file -
let myname = document.getElementById("showmyname");
let btn = document.getElementById("btn");
let btn1 = document.getElementById("btn1");
let timeout;
let num = 0
btn.addEventListener('click', () => {
myname.innerHTML = "loading....."
timeout = setInterval(() => {
myname.innerHTML = `${num}`
num++;
}, 1000)
})
btn1.addEventListener('click',()=>{
myname.innerHTML = `stopped! at ${num}`
clearInterval(timeout)
})
0 Comments