{"id":106,"date":"2009-01-26T11:57:16","date_gmt":"2009-01-26T16:57:16","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2009\/01\/26\/javascript-pause-function\/"},"modified":"2009-01-26T11:57:16","modified_gmt":"2009-01-26T16:57:16","slug":"javascript-pause-function","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2009\/01\/26\/javascript-pause-function\/","title":{"rendered":"Javascript pause function"},"content":{"rendered":"<p>The sample Javascript pause() function below performs a pause for X milliseconds.  During the time specified, the browser would appear &#8220;frozen&#8221; to the user.<\/p>\n<p>Quick reminder: 1000 milliseconds = 1 second, 60000 milliseconds = 1 minute<\/p>\n<pre class=\"code\">\nfunction pause(milliseconds) {\n\tvar dt = new Date();\n\twhile ((new Date()) - dt <= milliseconds) { \/* Do nothing *\/ }\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 pause(milliseconds) {\n\tvar dt = new Date();\n\twhile ((new Date()) - dt <= milliseconds) { \/* Do nothing *\/ }\n}\n\nfunction preStuff() {\n\talert('This is before the 3-second pause');\n}\n\nfunction postStuff() {\n\talert('This is after the pause; it should have been paused for 3 seconds');\n}\n\n<\/script><br \/>\n<input type=\"button\" onclick=\"javascript: preStuff(); pause(3000); postStuff();\" value=\"Do a 3-second pause\">\n<\/p>\n<pre class=\"code\"><xmp>\n<script type=\"text\/javascript\">\nfunction pause(milliseconds) {\n\tvar dt = new Date();\n\twhile ((new Date()) - dt <= milliseconds) { \/* Do nothing *\/ }\n}\n\nfunction preStuff() {\n\talert('This is before the 3-second pause');\n}\n\nfunction postStuff() {\n\talert('This is after the pause; it should have been paused for 3 seconds');\n}\n\n<\/script>\n\n<input type=\"button\" onclick=\"javascript: preStuff(); pause(3000); postStuff();\" value=\"Do a 3-second pause\">\n<\/xmp><\/pre>\n<p>If you are looking for a way to simply delay execution of code without \"freezing\" the browser during the wait time, try the <a href=\"\/code.php?q=80\">Javascript delay()<\/a> method.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes we need to freeze a web page for a 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-106","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/106","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=106"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}