{"id":3056,"date":"2016-01-22T01:37:37","date_gmt":"2016-01-22T01:37:37","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3056"},"modified":"2020-03-31T00:40:37","modified_gmt":"2020-03-31T00:40:37","slug":"lodash-debounce-example","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/lodash-debounce-example\/","title":{"rendered":"Lodash debounce example"},"content":{"rendered":"\n<p>To ensure that a JavaScript function is not called more than once every few seconds, you can run wrap it it in the &#8220;debounce&#8221; function available in lodash:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const run = () =&gt;\n   console.log('abc');\n\nconst lag = \n  _.debounce(run, 1500);\n\nlag();\nlag();\nlag();\n<\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">abc<\/pre>\n\n\n\n<p>In this case, the function will only get run once.<\/p>\n\n\n\n<p>If you pass an argument, it will be sent through to the function, but still only one call is made. This is why these work well as click handlers.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const run = (a) =&gt; \n   console.log(a);\n\nconst fn = \n  _.debounce(run, 1500);\n\nfn('a');\nfn('b');\nfn('c');<\/pre>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">c<\/pre>\n\n\n\n<p>By default, debounce calls the function at the end of the interval.<\/p>\n\n\n\n<p>If it to run at the beginning of the interval without a pause, do this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const fn = \n  _.debounce(run, 500, {leading: true});\n\nfn('a'); \nfn('b'); \nfn('c');<\/pre>\n\n\n\n<p>This will display the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This post shows how to use the lodash debounce function.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[302,343],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3056"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3056"}],"version-history":[{"count":2,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3056\/revisions"}],"predecessor-version":[{"id":6491,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3056\/revisions\/6491"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}