Javascript setInterval and setTimeout methods in Hindi

setTimeout and setInterval in Javascript

javascript में setTimeout और setInterval दो pre-defined methods है | जब हमे किसी code को एक particular time के बाद execute करना होता है, तब इन दोनों methods का उपयोग किया जाता है |

setTimeout()

जब हम किसी javascript code कुछ time interval के बाद execute करना चाहतें हैं, तब setTimeout() method का उपयोग करतें हैं | setTimeout() दी गयी time interval में code को एक ही बार execute करता है |

Syntax

setTimeout(functionName, timeduration);

functionName parameter में उस method को लिखा जाता है जिसे कुछ समय के अंतराल के बाद call करना है |

timeDuration parameter में time दिया जाता है कितनी duration के बाद function call होगा | timeduration milliseconds में दे सकतें हैं |

1sec = 1000 milliseconds होता है |

Example

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Set Interval Example</title>
    </head>
    <body>
        <h3 id="heading"></h3>
        <script>
             var a = "Hi You will get a message";
             document.querySelector("#heading").innerHTML = a;

             function showMsg()
             {
               a = "Have a Wonderful Day";
               document.querySelector("#heading").style.color = "Red";
               document.querySelector("#heading").innerHTML = a;
             }
             setTimeout(showMsg,2000);
        </script>    
    </body>
</html>

उदाहरण में देखिए setTimeout में showMsg() function को पास किया गया है जिस वजह से उसके अंदर का code 2sec के बाद execute हुआ |

setInterval()

जब हम किसी javascript code को किसी time blocks के अंतराल में बार बार execute करना चाहतें हैं, तब setInterval() method का उपयोग करतें हैं |

Syntax

setInterval(functionName, timeduration);

उदाहरण

<h3 id="heading"></h3>
        <script>            
             function showTime(){
                 let a = new Date();
                 document.querySelector("#heading").style.color = "Red";
                 document.querySelector("#heading").innerHTML = "Time is: " + a.toLocaleTimeString();
             }
             setInterval(showTime,2000);
        </script> 

clearTimeout()

setTimeout() में call हो रहे method को रोकने के लिए clearTimeout() का उपयोग होता है |

Syntax: clearTimeout(setTimeout_function_name);

उदाहरण: clearTimeout(showTimeOut);

clearInterval()

setInterval() में call हो रहे method को रोकने के लिए clearIntervalt() का उपयोग होता है |

Syntax: clearInterval(setInterval_function_name);

clearInterval() method के अंदर उस setInterval() method की reference यानि नाम को डाली जाती है जिसे रोकना चाहतें हैं |

उदाहरण:

      <script>
         var count = 0;
         function showCountDown(){
             count++;
             console.log("Count is " + count); 
             if(count == 5)
             {
               clearInterval(s);
             }               
           }
          var s = setInterval(showCountDown, 2000);
        </script>

उदाहरण में देखिए showCountDown() method हर 2 second में execute होता रहेगा | इसीलिए condition लगाकर 5 count के बाद clearInterval() के जरिए इसे रोकी गयी है |

अन्य tutorial के सुझाव