setTimeout()
यह कई टाइमिंग इवेंट्स में से एक है। विंडो ऑब्जेक्ट निर्दिष्ट समय अंतराल पर कोड के निष्पादन की अनुमति देता है। इस ऑब्जेक्ट ने setTimeout() प्रदान किया है जो एक निश्चित समय के बाद फंक्शन को निष्पादित करता है। यह दो पैरामीटर्स लेता है। एक फंक्शन है और दूसरा वह समय है जो उस अंतराल को निर्दिष्ट करता है जिसके बाद फंक्शन को निष्पादित किया जाना चाहिए।
सिंटैक्स
window.setTimeout(function, milliseconds);
उदाहरण-1
निम्नलिखित उदाहरण में, setTimeout() फंक्शन को दिया गया समय 2 सेकंड है। इसलिए फंक्शन दो सेकंड के बाद निष्पादित होगा और आउटपुट दिखाएगा।
<html> <body> <p>wait 2 seconds.</p> <script> setTimeout(function(){ document.write("Tutorix is the best e-learning platform"); }, 2000); </script> </body> </html>
आउटपुट
wait 2 seconds Tutorix is the best e-learning platform
उदाहरण-2
निम्नलिखित उदाहरण में, setTimeout() फंक्शन को दिया गया समय 3 सेकंड है। इसलिए फंक्शन तीन सेकंड के बाद निष्पादित होगा और आउटपुट दिखाएगा।
<html> <body> <p id = "time"></p> <script> setTimeout(function(){ document.getElementById("time").innerHTML = "Tutorix is the product of Tutorialspoint"; }, 3000); </script> </body> </html>
आउटपुट
Tutorix is the product of Tutorialspoint.