JS setTimeout और setInterval
In this page:
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()
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
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()
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()
// 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()
// 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);
- एक call pass करना, जैसे
setTimeout(greet(), 1000), जो तुरंत चलता है;greetpass करें। clearInterval()कॉल करना भूल जाना, इसलिए interval हमेशा के लिए चलता रहता है।- यह उम्मीद करना कि delay exact होगी, जबकि यह एक minimum time है और बाकी code इसे delay कर सकता है।
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS JSON
- JS Regular Expressions
- JS Fetch API
- JS LocalStorage and SessionStorage
- JS Cookies
- JS setTimeout and setInterval
- JS Event Loop
- JS Web Workers
- JS Service Workers
- JS AJAX
- JS AJAX Intro
- JS AJAX XMLHttp
- JS AJAX Request
- JS AJAX Response
- JS AJAX XML
- JS AJAX PHP
- JS AJAX Database
- JS JSONP
- JS RegExp Flags
- JS RegExp Classes
- JS RegExp Metachars
- JS RegExp Assertions
- JS RegExp Groups
- JS RegExp Quantifiers
- JS JSON HTML
- JS JSON vs XML