JavaScript me setTimeout function ka use kaise karen udahran ke saath

#54
24
1 Answer
Question Image

JavaScript me setTimeout() function ka use kaise karen udahran ke saath bataen


Related to:
javascriptsettimeoutfunction
0
Answer
Answer 1 of 1
# 41

JavaScript me setTimeout ko use karne ke kuchh udahran dekhiye:

1. Ek alert ke baad doosra alert

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.

 

Yeh kaise kaam karta 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.

 

2. Ek message ko display karna aur phir hide karna:

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.

 

3. Ek function ko repeat karna:

function repeatFunction() {
  console.log("Hello World!");
  setTimeout(repeatFunction, 1000);
}
repeatFunction();

Is code mein, ek function ko repeat kiya jata hai har 1 second mein.

 

4. Ek animation ko create karna:

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.

 

5. Ek countdown timer ko create karna:

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

0
0
0
Related Articles

If you still have a question about this, submit it in our Q&A community - Ask Question