{"id":6709,"date":"2014-09-05T19:28:28","date_gmt":"2014-09-05T17:28:28","guid":{"rendered":"https:\/\/www.creativejuiz.fr\/blog?p=6709"},"modified":"2019-02-24T20:48:03","modified_gmt":"2019-02-24T19:48:03","slug":"more-performant-onresize-onscroll-javascript","status":"publish","type":"post","link":"https:\/\/www.creativejuiz.fr\/blog\/en\/tutorials\/more-performant-onresize-onscroll-javascript","title":{"rendered":"A more performant &#8220;onresize&#8221; and &#8220;onscroll&#8221; in JS"},"content":{"rendered":"<p>If you are a frequently user of JS, you have certainly noticed that some events are triggered quite occasionally, and others can be triggered very frequently and become quite difficult to manage, as in the case of &#8220;onscroll&#8221; and &#8220;onresize&#8221; for example.<\/p>\n<p><!--more--><\/p>\n<p>When you implement an event listener on one of these events (I&#8217;ll take <code>onresize<\/code> for the following examples) you&#8217;ll quickly see when resizing the window that they are executed each time the pixel changes within the window width.<\/p>\n<p class=\"message\"><strong>2016 Update<\/strong>: Take a look at <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/window\/requestAnimationFrame\"><code>window.requestAnimationFrame()<\/code><\/a> which can help a lot too.<\/p>\n<h2>The code used most of the time<\/h2>\n<p>Often, and quite simply, we implement a listener like this:<\/p>\n<pre class=\"code\"><code class=\"language-javascript\">window.addEventListener('resize', function(){\r\n  console.log('resizing');\r\n});<\/code><\/pre>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-4171\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2014\/08\/resize-sucks-js.gif\" alt=\"\" width=\"319\" height=\"192\" \/>If you add this to the JS scripts of your project, and open the page on your browser, you will notice when you open your console (F12) and resize the window, that the console panics a little.<br \/>\nImagine that instead of a <code>console.log()<\/code> your script checks the DOM, changes the structure under certain conditions (screen width, visibility of an element, for example), etc., and this for each pixel of the resizing&#8230;.<\/p>\n<p>Don&#8217;t laugh, we agree that resizing the window like this is not common for the user, but it was (or still is) a client&#8217;s method to test if the responsive was working well, and he thought it was a little slow on his Mac. I don&#8217;t blame him, he probably read somewhere that the responsive is when you resize your browser window.<\/p>\n<p>In short, we can optimize the execution frequency of all this code when resizing, let&#8217;s see that.<\/p>\n<h2>The same function, but a bit different<\/h2>\n<p>The principle is to create a <code>timeout<\/code> that is run at resizing and destroyed at resizing. Yeah, I know, it&#8217;s weird, but you&#8217;ll understand.<\/p>\n<pre class=\"code\"><code class=\"language-javascript\">var the_timer;\r\n\u00a0\r\nwindow.addEventListener('resize', function(){\r\n  clearTimeout(the_timer);\r\n  the_timer = setTimeout(function(){\r\n    console.log('resized');\r\n  }, 75);\r\n});<\/code><\/pre>\n<p>To give you a short explanation, just read the code in a linear way: when resizing, we delete the <code>timeout<\/code> (this stops the execution of the function as a parameter), then we launch the <code>timeout<\/code>. This has an execution delay of 75 milliseconds. So, if less than 75 ms have elapsed between the first <code>resize<\/code> trigger and the second, the <code>timeout<\/code> is reset even before the function is executed in the first parameter. Did you follow me?<br \/>\nWhat makes it work is the (global) <code>the_timer<\/code> variable declared before the listener.<\/p>\n<p><img decoding=\"async\" class=\"alignleft size-full wp-image-4174\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2014\/08\/resize-js-ok.gif\" alt=\"\" width=\"319\" height=\"192\" \/>There you go, it&#8217;s a little better though. The frequency, or &#8220;sensitivity&#8221; is set to the number of milliseconds (here <code>75<\/code>) entered for the <code>timeout<\/code>. The higher this number, the faster the content of your function will be executed.<\/p>\n<p>This method is not necessarily adapted to all cases for all projects, especially in the context of the <code>onscroll<\/code> event, so measure the impact of this implementation well \ud83d\ude42<\/p>\n<p>I hope this tip will be useful to you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are a frequently user of JS, you have certainly noticed that some events are triggered quite occasionally, and others can be triggered very frequently and become quite difficult to manage, as in the case of &#8220;onscroll&#8221; and &#8220;onresize&#8221; for example.<\/p>\n","protected":false},"author":4,"featured_media":6711,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bluesky_dont_syndicate":"","_bluesky_syndication_accounts":"","_bluesky_syndication_text":"","footnotes":""},"categories":[657,647],"tags":[787],"coauthors":[597],"class_list":["post-6709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-en","category-tutorials","tag-performance-en"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/comments?post=6709"}],"version-history":[{"count":3,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6709\/revisions"}],"predecessor-version":[{"id":6720,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6709\/revisions\/6720"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media\/6711"}],"wp:attachment":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media?parent=6709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/categories?post=6709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/tags?post=6709"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/coauthors?post=6709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}