Syntax Error In JavaScript In Hindi | JS SyntaxError
Indexed DB In JavaScript In Hindi | JS Indexed DB In Hindi
JavaScript Object keys, values, entries : Object methods in JS
Generator Functions In JavaScript In Hindi | JavaScript Generator Functions
JavaScript "new Function" Syntax : JS "new Function" Syntax
What is DHCP In Hindi? DHCP kya hai?
Java program to check if string is a palindrome : check palindrome string in java
aws s3 kya hai ? | what is aws s3
Java program to check if a number is Armstrong : Find Armstrong Number In Java
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
अगर आपने हमारी website पर कुछ blogs पढ़े होंगे तो कई जगह पर setTimeout()
function का use देखा होगा , और आज इस blog में इसी function के बारे में detail से पढ़ेंगे।
Actually कई बार हमें ऐसी जरूरत पड़ती हैं जहाँ हम किसी block of code / function को तुरंत execute न करके कुछ time बाद execute कराना चाहते हैं , ऐसी situation में हम setTimeout()
function का use करते हैं।
let time_id = setTimeout(callback_function, [delay in miliseconds], [arg1], [arg2], ...)
Explanation
callback_function : setTimeout() function का पहला argument एक Callback Function है , जिसे या तो आप पहले से defined किसी function का नाम या directly function pass कर सकते हैं।
delay : delay वह time है जिसके बाद आप callback function run करना चाहते हैं , इसकी value milliseconds
में होती है।
Argument list : arg1 , arg2 , arg3 . . . वो arguments हैं जिन्हे आप callback functions में handle करना चाहते हैं।
●●●
Example के लिए हम simple एक normal function pass करेंगे जिसमे 'Hello' print कराएँगे।
function sayHello() {
alert('Hello');
}
// delay for 5 seconds only.
setTimeout(sayHello, 5000);
Output
Hello
ऊपर दिए गए example को अगर आप run करेंगे तो setTimeout()
function में pass किया sayHello()
function 5 seconds बाद ही run होगा।
हालाँकि अगर आप चाहे तो , अलग से function define न करके directly setTimeout()
function में pass कर सकते हैं। ऐसा करने से आपके output में कोई फर्क नहीं आएगा।
// pass function as an argument directly.
setTimeout(function(){
alert('Hello');
}, 5000);
Output
Hello
●●●
अब हो सकता है कि setTimeout()
function में pass किये जाने वाले Callback Function में हमें arguments handle की जरूरत पड़ जाए।
ऐसे में आप syntax के according delay
के बाद उन arguments को pass कर सकते हैं।
function sayHello(fname, lname) {
alert(`Hello ${fname} ${lname}`);
}
// delay for 2 seconds only.
setTimeout(sayHello, 2000, "Babu", "Bhaiya");
Output
Hello Babu Bhaiya
अब अगर कोई single line statement है तो आप complete function pass करने की वजाय Arrow Function का use भी कर सकते हैं।
setTimeout(() => alert("Hello"), 2000);
Output
Hello
●●●
तो setTimeout()
function एक एक timerId
return करता है , जिसका use करके हम setTimeout() function के execution को cancel कर सकते हैं।
let timerId = setTimeout(() => alert("It will never happen"), 1000);
console.log(timerId); // 3
clearTimeout(timerId);
timerId का output आपके system पर अलग अलग हो सकता है।
I Hope , अब आपको समझ आ गया होगा कि JavaScript में setTimeout()
function क्या है और कैसे काम करता है।
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook