{"id":437,"date":"2019-02-12T00:25:45","date_gmt":"2019-02-12T04:25:45","guid":{"rendered":"https:\/\/benjaminray.com\/codebase\/?p=437"},"modified":"2019-02-12T00:30:09","modified_gmt":"2019-02-12T04:30:09","slug":"pass-parameter-to-settimeout-in-javascript","status":"publish","type":"post","link":"https:\/\/benjaminray.com\/codebase\/pass-parameter-to-settimeout-in-javascript\/","title":{"rendered":"Pass Parameter to SetTimeout in JavaScript"},"content":{"rendered":"<p>It is sometimes necessary to pass a variable to a SetTimeout() callback function. By the time the callback occurs, the original variable may no longer exist, so passing a copy of it as a third parameter in SetTimeout() solves this issue.<\/p>\n<p>In this case, we want the callback function to output the value of <code>name<\/code> as it was at the time the callback was triggered (\"My Original Name\"), and not the value at the time the callback occurred (\"Some New Name\").<\/p>\n<pre><code>var name = \"My Original Name\";\nsetTimeout(function(x) {\n    console.log(x);\n}, 1000, name);\nname = \"Some New Name\";\n\n\/\/ Output: My Original Name\n<\/code><\/pre>\n<p>If we didn't pass the value in as a variable, the callback would reference the value of <code>name<\/code> at the time of the callback, by which point it has been changed to \"Some New Name\".<\/p>\n<p>More information: <a href=\"https:\/\/stackoverflow.com\/a\/7503942\/4669143\" rel=\"noopener\" target=\"_blank\">https:\/\/stackoverflow.com\/a\/7503942\/4669143<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is sometimes necessary to pass a variable to a SetTimeout() callback function. By the time the callback occurs, the original variable may no longer exist, so passing a copy  [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-437","post","type-post","status-publish","format-standard","hentry","category-javascript"],"acf":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9GNjN-73","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/437","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/comments?post=437"}],"version-history":[{"count":3,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/posts\/437\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjaminray.com\/codebase\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}