Ternary Operator In JavaScript In Hindi | JavaScript Conditional Operator In Hindi
Important JavaScript Interview Questions In Hindi
what are callback functions in JavaScript
JavaScript Spread syntax In Hindi : Spread Operator In JavaScript
Password Strength Checker In jQuery In Hindi
PHP object Type Casting In Hindi | PHP object In Hindi
Linux ls command and it's options
What is topology In Hindi ? Topology kya hai ?
Laravel echo, socket io and redis with example : Laravel socket io with redis
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.
Image on pixabay
अगर आपने हमारी 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 ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook