Understanding setInterval In JavaScript | JS setInterval In Hindi

Image could not load

JS setInterval

आपने हमारी website पर कई examples में आपने setInterval() function का use देखा होगा , और आज इस blog में इसी function के बारे में detail से पढ़ेंगे।

JS setInterval In Hindi

Actually कई बार हमें ऐसी जरूरत पड़ती हैं जहाँ हम किसी block of code / function को किसी particular time के बाद repeatedly execute कराना चाहते हैं , ऐसी situation में हम setInterval() function का use करते हैं।

actually इसे एक तरह से Scheduler की तरह use किया जाता है , जहाँ दिए गए time के बाद block of code run होता रहता है।

JS setInterval Syntax
let time_id = setInterval(callback_function, [delay in miliseconds], [arg1], [arg2], ...);

Explanation

  • callback_function : setInterval() function का पहला argument एक Callback Function है , जिसे या तो आप पहले से defined किसी function का नाम या directly function pass कर सकते हैं।

  • delay : delay वह time है जिसके बाद आप callback function run करना चाहते हैं , इसकी value milliseconds में होती है। जैसे आपने 10 second time set किया तो हर 10 second में pass किया गया callback function run होता रहेगा।

  • Argument list : arg1 , arg2 , arg3 . . . वो arguments हैं जिन्हे आप callback functions में handle करना चाहते हैं।

JS setInterval Example

function sayHello() { console.log('Hello'); } // delay for 2 seconds only. setInterval(sayHello, 2000);

Output

Hello 
Hello // after 2 seconds 
Hello // after 4 seconds 
Hello // after 6 seconds 
. . and so on.

example में हमें 2 seconds का delay किया है तो जब तक code remove नहीं कर देते या tab close नहीं कर देते तब तक हर 2 second बाद sayHello() function call होता रहेगा।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

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