← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 6 of 26

JS setTimeout और setInterval

setTimeout किसी इंतज़ार के बाद एक बार कुछ code चलाता है, जैसे एक oven timer, और setInterval उसे बार-बार दोहराता है, जैसे एक घड़ी की टिक-टिक। आप दोनों में से किसी को भी cancel कर सकते हैं।
Syntax
javascript
const timeoutId = setTimeout(function, milliseconds);
const intervalId = setInterval(function, milliseconds);
clearTimeout(timeoutId);
clearInterval(intervalId);

setTimeout()

setTimeout() किसी function को एक delay के बाद चलने के लिए schedule करता है। delay एक minimum है, कोई गारंटी नहीं — अगर call stack busy हो, तो callback delay बीतने के बाद भी JavaScript के free होने तक इंतज़ार करता है।

उदाहरण: setTimeout()

javascript
setTimeout(() => {
  // Print "Runs after at least 1 second" to the console
  // Print "Runs after at least 1 second" to the console
  console.log("Runs after at least 1 second");
}, 1000);

Arguments पास करना

आप setTimeout() से schedule किए गए किसी function को arguments पास कर सकते हैं। delay के बाद के कोई भी arguments callback function को उसके आखिरकार चलने पर पास कर दिए जाते हैं, जिससे एक अतिरिक्त wrapper closure की ज़रूरत नहीं पड़ती।

उदाहरण: Passing Arguments

javascript
setTimeout((name) => {
  // Print `"Hello, " + name` to the console
  // Print `"Hello, " + name` to the console
  console.log("Hello, " + name);
}, 500, "Sam");

clearTimeout()

clearTimeout() किसी timeout को उसके चलने से पहले cancel कर देता है। delay बीतने से पहले clearTimeout() कॉल करना scheduled callback को कभी न चलने देता है, जो किसी component के unmount होने या user द्वारा कोई action cancel करने पर cleanup के लिए ज़रूरी है।

उदाहरण: clearTimeout()

javascript
const id = setTimeout(() => console.log("This will never run"), 1000);
clearTimeout(id);

setInterval()

setInterval() हर दिए गए delay के बाद एक function दोहराता है। setTimeout के one-time delay के उलट, setInterval() उस interval पर callback को बार-बार invoke करता रहता है जब तक कोई इसे explicitly न रोके।

उदाहरण: setInterval()

javascript
// Declare the variable `count`, set to `0`
// Declare the variable `count`, set to `0`
let count = 0;
const id = setInterval(() => {
  count++;
  // Print `count` to the console
  // Print `count` to the console
  console.log(count);
}, 500);

clearInterval()

clearInterval() किसी repeating timer को रोक देता है। किसी interval को clear करना भूल जाना memory leaks और अनचाहे background काम का एक आम कारण है, इसलिए हर setInterval() के साथ कहीं न कहीं एक matching clearInterval() होना चाहिए।

उदाहरण: clearInterval()

javascript
// Declare the variable `count`, set to `0`
// Declare the variable `count`, set to `0`
let count = 0;
const id = setInterval(() => {
  count++;
  // Print `count` to the console
  // Print `count` to the console
  console.log(count);
  if (count === 3) clearInterval(id);
}, 300);
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक call pass करना, जैसे setTimeout(greet(), 1000), जो तुरंत चलता है; greet pass करें।
  2. clearInterval() कॉल करना भूल जाना, इसलिए interval हमेशा के लिए चलता रहता है।
  3. यह उम्मीद करना कि delay exact होगी, जबकि यह एक minimum time है और बाकी code इसे delay कर सकता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.