JavaScript me setTimeout() function ka use kaise karen udahran ke saath bataen
JavaScript me setTimeout ko use karne ke kuchh udahran dekhiye:
alert("Pehla alert");
setTimeout(function() {
alert("Doosra alert");
}, 20000);
Is code mein, setTimeout function ko use kiya gaya hai, jo ki ek function ko delay se execute karta hai. Is case mein, doosre alert ko 20 second (20000 milliseconds) ke baad execute kiya jayega hai.
1. Pehla alert display hota hai.
2. setTimeout function ko call kiya jata hai, jo ki doosre alert ko delay se execute karta hai.
3. 20 second ke baad, doosra alert display hota hai.
document.getElementById("message").innerHTML = "Hello World!";
setTimeout(function() {
document.getElementById("message").innerHTML = "";
}, 3000);
Is code mein, ek message ko display kiya jata hai aur phir 3 second ke baad hide kiya jata hai.
function repeatFunction() {
console.log("Hello World!");
setTimeout(repeatFunction, 1000);
}
repeatFunction();
Is code mein, ek function ko repeat kiya jata hai har 1 second mein.
var i = 0;
function animate() {
document.getElementById("animation").style.left = i + "px";
i += 10;
if (i < 100) {
setTimeout(animate, 16);
}
}
animate();
Is code mein, ek animation ko create kiya jata hai jo ki ek element ko left se right tak move karta hai.
var seconds = 10;
function countdown() {
document.getElementById("timer").innerHTML = seconds + " seconds";
seconds--;
if (seconds >= 0) {
setTimeout(countdown, 1000);
} else {
document.getElementById("timer").innerHTML = "Time's up!";
}
}
countdown();
Is code mein, ek countdown timer ko create kiya jata hai jo ki 10 second tak countdown karta hai.
Yeh udaharan dikhaate hain ki kaise setTimeout function ka use kiya jata hai, aur yeh information aapko aur bhi creative tareeke se use karne mein madad karega!
Adhik jankari ke liye related articles dekhen
If you still have a question about this, submit it in our Q&A community - Ask Question