{"id":460,"date":"2023-05-17T17:19:00","date_gmt":"2023-05-17T17:19:00","guid":{"rendered":"https:\/\/www.devopssupport.in\/blog\/?p=460"},"modified":"2023-05-17T07:36:09","modified_gmt":"2023-05-17T07:36:09","slug":"settimeout-in-javascript","status":"publish","type":"post","link":"https:\/\/www.devopssupport.in\/blog\/settimeout-in-javascript\/","title":{"rendered":"setTimeout() in JavaScript"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17-1024x512.png\" alt=\"\" class=\"wp-image-461\" width=\"466\" height=\"233\" srcset=\"https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17-1024x512.png 1024w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17-300x150.png 300w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17-768x384.png 768w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17-850x425.png 850w, https:\/\/www.devopssupport.in\/blog\/wp-content\/uploads\/2023\/05\/image-17.png 1200w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n\n\n\n<p>In JavaScript, asynchronous programming plays a vital role in creating interactive and responsive web applications. The <code>setTimeout()<\/code> function is a key component in this realm, allowing developers to introduce delays and schedule the execution of code. <\/p>\n\n\n\n<p>The <code>setTimeout()<\/code> function is a built-in method in JavaScript that allows you to schedule the execution of a function or a code block after a specified delay. It is primarily used for introducing time delays or implementing timeout functionality.<\/p>\n\n\n\nsetTimeout(function, delay, param1, param2, &#8230;);\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>function<\/code> <\/strong>(required): The function parameter represents the code or the function that you want to execute after the specified delay. It can be an existing function or an anonymous function.<\/li>\n\n\n\n<li><strong><code>delay<\/code> <\/strong>(required): The delay parameter specifies the time interval, in milliseconds, after which the code or function will run.<\/li>\n\n\n\n<li><code><strong>param1<\/strong>, <strong>param2<\/strong>, ...<\/code> (optional): Additional parameters can be passed after the delay parameter, which will be supplied as arguments to the function being executed. This allows you to dynamically pass values to the function when it runs.<\/li>\n<\/ol>\n\n\n\n<p>Example: <\/p>\n\n\n\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>Document<\/title>\n<\/head>\n<body>\n    <H1>This is  setTimeout <\/H1>\n    <script type=\"text\/javascript\">  \n        function msg(){  \n        setTimeout(  \n        function(){  \n        alert(\"Welcome to setTimeout  after 2 seconds\")  \n        },2000);  \n          \n        }  \n        <\/script>  \n          \n        <input type=\"button\" value=\"click\" onclick=\"msg()\"\/>  \n<\/body>\n<\/html>\n\n\n\n<p>In this example, <\/p>\n\n\n\n<p>there is a function declaration <code>msg()<\/code>. This function is invoked when the button is clicked.<\/p>\n\n\n\n<p>Within the <code>msg()<\/code> function, there is a <code>setTimeout()<\/code> call. <code>setTimeout()<\/code> is a built-in JavaScript function that allows for delayed execution. In this example, it is used to schedule an alert box to appear after a delay of 2000 milliseconds (2 seconds). The anonymous function passed to <code>setTimeout()<\/code> displays the &#8220;Welcome to setTimeout after 2 seconds&#8221; message using the <code>alert()<\/code> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, asynchronous programming plays a vital role in creating interactive and responsive web applications. The setTimeout() function is a key component in this realm, allowing developers&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[180,181],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-settimeout","tag-settimeout-functio"],"_links":{"self":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/comments?post=460"}],"version-history":[{"count":1,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":462,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions\/462"}],"wp:attachment":[{"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopssupport.in\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}