{"id":128,"date":"2010-04-06T13:07:29","date_gmt":"2010-04-06T17:07:29","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2010\/04\/06\/javascript-delay-function\/"},"modified":"2010-04-06T13:07:29","modified_gmt":"2010-04-06T17:07:29","slug":"javascript-delay-function","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2010\/04\/06\/javascript-delay-function\/","title":{"rendered":"Javascript delay function"},"content":{"rendered":"<p>The sample Javascript delay() function below performs a delay for X milliseconds.  During the time specified, users can proceed to perform actions on the particular web page.<\/p>\n<p>Quick reminder: 1000 milliseconds = 1 second, 60000 milliseconds = 1 minute<\/p>\n<pre class=\"code\">\nfunction delay(milliseconds) {\n\tvar cmd = 'alert(\"Hello World!  This should have appeared after ' + milliseconds + ' milliseconds.\");';\n\tsetTimeout(cmd, milliseconds);\n}\n<\/pre>\n<p>Below is a live example, followed by the HTML\/Javascript code used.<\/p>\n<p class=\"code\">\n<script type=\"text\/javascript\">\nfunction delay(milliseconds) {\n\tvar cmd = 'alert(\"Hello World!  This should have appeared after ' + milliseconds + ' milliseconds.\");';\n\tsetTimeout(cmd, milliseconds);\n}\n<\/script><br \/>\n<input type=\"button\" onclick=\"javascript: delay(3000);\" value=\"Do a 3-second delay\">\n<\/p>\n<pre class=\"code\"><xmp>\n<script type=\"text\/javascript\">\nfunction delay(milliseconds) {\n\tvar cmd = 'alert(\"Hello World!  This should have appeared after ' + milliseconds + ' milliseconds.\");';\n\tsetTimeout(cmd, milliseconds);\n}\n<\/script>\n<input type=\"button\" onclick=\"javascript: delay(3000);\" value=\"Do a 3-second delay\">\n<\/xmp><\/pre>\n<p>If you are looking for a way to &#8220;freeze&#8221; the browser during the wait time, try the <a href=\"\/code.php?q=60\">Javascript pause()<\/a> method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we need to delay the execution of Javascript code for certain amount of time.  This simple example allows that functionality.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}