{"id":5031,"date":"2013-08-30T02:00:00","date_gmt":"2013-08-30T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/firing-resize-event-only-once-when-resizing-is-finished\/"},"modified":"2024-02-08T00:30:12","modified_gmt":"2024-02-07T23:30:12","slug":"firing-resize-event-only-once-when-resizing-is-finished","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/firing-resize-event-only-once-when-resizing-is-finished\/","title":{"rendered":"JQuery Resize Event After Resize Ends [JavaScript &#038; jQuery]"},"content":{"rendered":"<p><a href=\"http:\/\/api.jquery.com\/resize\/\" target=\"_blank\" rel=\"noopener nofollow\">jQuery resize event<\/a> (or Javascript one) is fired when the size of the browser&#8217;s window (viewport) changes as pointed out in jQuery documentation.<\/p>\n<p>Sometimes we need to execute functions which might take a while to execute or which might consume quite a few resources from the machine. For those cases we don&#8217;t usually want to execute them tens of times while the user is still re-sizing the window to reach the desired size.<\/p>\n<p>For these particular cases we can use a very simple trick:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">var<\/span> resizeId;\n$(<span class=\"hljs-built_in\">window<\/span>).resize(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    clearTimeout(resizeId);\n    resizeId = setTimeout(doneResizing, <span class=\"hljs-number\">500<\/span>);\n});\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">doneResizing<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n    <span class=\"hljs-comment\">\/\/whatever we want to do<\/span>\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n\n<div class=\"wp-block-button\">\n<a class=\"wp-block-button__link wp-element-button\" href=\"http:\/\/jsfiddle.net\/Zevan\/c9UE5\/1\/\" target=\"_blank\" title=\"See Demo\" rel=\"noopener nofollow\">See Demo<\/a>\n<\/div>\n\n<\/div>\n\n<p>Basically what we do in this case is adding a timeout of 500 milliseconds to call our function <code>doneResizing<\/code>.<br>\nThis counter of 500 milliseconds gets restarted with every change in the window dimensions (as we clear the timeout) so the function will only be called if the user stops resizing the window OR if the user resizes the window VERY slowly, which is very unlikely \ud83d\ude42<\/p>\n<p>I&#8217;m personally making use of this trick on my <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js library<\/a> because it does a few changes on the DOM on resize. You can see it implemented <a href=\"https:\/\/github.com\/alvarotrigo\/fullPage.js\/blob\/master\/src\/fullpage.js#L2456\" target=\"_blank\" rel=\"noopener nofollow\">here<\/a>.<\/p>\n<p>In case you want to mimic the same behaviour in <strong>vanilla Javascript<\/strong>, here&#8217;s the equivalent:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">var<\/span> resizeId;\n<span class=\"hljs-built_in\">window<\/span>.addEventListener(<span class=\"hljs-string\">'resize'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n    clearTimeout(resizeId);\n    resizeId = setTimeout(doneResizing, <span class=\"hljs-number\">500<\/span>);\n});\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">doneResizing<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n    <span class=\"hljs-comment\">\/\/whatever we want to do<\/span>\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>Here&#8217;s a working example. You can test it by opening <a href=\"https:\/\/codepen.io\/alvarotrigo\/pen\/xxLwJzJ\" target=\"_blank\" rel=\"noopener nofollow\">the codepen<\/a> on a new window and resizing the right frame.<\/p>\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_xxLwJzJ\" data-src=\"\/\/codepen.io\/anon\/embed\/xxLwJzJ?height=450&amp;theme-id=1&amp;slug-hash=xxLwJzJ&amp;default-tab=js,result\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed xxLwJzJ\" title=\"CodePen Embed xxLwJzJ\" class=\"cp_embed_iframe lazyload\" style=\"width:100%;overflow:hidden\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" data-load-mode=\"1\">CodePen Embed Fallback<\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>jQuery resize event (or Javascript one) is fired when the size of the browser\u2019s window (viewport) changes as pointed out in jQuery documentation.<\/p>\n","protected":false},"author":1,"featured_media":5030,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[107,120],"tags":[10,18],"class_list":["post-5031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","category-jquery","tag-javascript","tag-jquery"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5031"}],"version-history":[{"count":3,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5031\/revisions"}],"predecessor-version":[{"id":8766,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5031\/revisions\/8766"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5030"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}